Android Google Maps Activity won't start - android

I'm working on an Android app that should start a MapView when a tour is selected from a list. However The app seems to crash everytime I try to open this MapView via an Intent...
Here is my code:
MainActivity.java:
public class MainActivity extends Activity {
public final static String SELECTED_TOUR = "com.me.tourapp.TOUR";
public static TourListAdapter tourListAdapter;
private static int selectedTour;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
public void startTour(View view) {
Intent intent = new Intent(view.getContext(), TourMapActivity.class);
intent.putExtra(SELECTED_TOUR, selectedTour);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
}
}
TourMapActivity.java:
public class TourMapActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tour_map);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
//check if tour is passed via intent (test):
Intent intent = getIntent();
int tourIndex = Integer.parseInt(intent.getStringExtra(MainActivity.SELECTED_TOUR));
Toast.makeText(getApplicationContext(), "the tour: "+ tourIndex, Toast.LENGTH_LONG).show();
}
}
activity_tour_map.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
and some snippets from the manifest:
<permission
android:name="com.me.tourapp.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.me.tourapp.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-library android:name="com.google.android.maps" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBqs_*****"/>
I also included android-support-v4.jar to the java build path and installed and imported the google play services...
I just don't hava a clue where I'm making the errors...
edit:
I added the logcat:
E/AndroidRuntime(28248): FATAL EXCEPTION: main
E/AndroidRuntime(28248): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hansvn.plaktour/com.hansvn.plaktour.TourMapActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
E/AndroidRuntime(28248): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
E/AndroidRuntime(28248): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
E/AndroidRuntime(28248): at android.app.ActivityThread.access$600(ActivityThread.java:140)
E/AndroidRuntime(28248): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
E/AndroidRuntime(28248): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(28248): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(28248): at android.app.ActivityThread.main(ActivityThread.java:4898)
E/AndroidRuntime(28248): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(28248): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(28248): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
E/AndroidRuntime(28248): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
E/AndroidRuntime(28248): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(28248): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
E/AndroidRuntime(28248): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
E/AndroidRuntime(28248): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime(28248): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(28248): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(28248): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime(28248): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
E/AndroidRuntime(28248): at android.app.Activity.setContentView(Activity.java:1924)
E/AndroidRuntime(28248): at com.hansvn.plaktour.TourMapActivity.onCreate(TourMapActivity.java:16)
E/AndroidRuntime(28248): at android.app.Activity.performCreate(Activity.java:5206)
E/AndroidRuntime(28248): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
E/AndroidRuntime(28248): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
E/AndroidRuntime(28248): ... 11 more
E/AndroidRuntime(28248): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
E/AndroidRuntime(28248): at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
E/AndroidRuntime(28248): at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
E/AndroidRuntime(28248): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
E/AndroidRuntime(28248): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
E/AndroidRuntime(28248): ... 21 more
E/AndroidRuntime(28248): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
E/AndroidRuntime(28248): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
E/AndroidRuntime(28248): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime(28248): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime(28248): at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
E/AndroidRuntime(28248): ... 24 more

You messing API V2 with API V1 of Google Maps. So first of all decide which API are you going to use.
Next, if you have decided that you are using API V2 (as it looks by most of your code), then remove this line:
<uses-library android:name="com.google.android.maps" />
It's part of API V1 and doesn't need to be in V2.
Second thing you need to decide is what is the minAPI level of your application. If you are writing your application to API level < 11 then you should use SupportMapFragment in your XML, like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
and your activity should extend FragmentActivity, this is the case were you should use
the google-support-v4 package, because FragmentActivity is part of this package.
UPDATE:
From your logcat output:
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.me.tourapp/com.me.tourapp.TourMapActivity}; have you declared this activity in your AndroidManifest.xml?
which means you have not declared your Activity TourMapActivity in the manifest file, and this is the reason for your error.
UPDATE2:
this error:
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public.
derived as said in the error from the fact that SupportMapFragment class was not found.
what means that you haven't reference the google-play-services library the right way because this class is part of this library. please read the first 3 steps of this blog post I wrote on how to reference it correctly:
Google Map API V2

Related

'Error inflating class fragment' error when following Maps API tutorial

I've been following the Xamarin Maps API tutorial and am consistantly running into problems. The most consistent of which is 'Android.Views.InflateException'. I have followed the toturial to the absolute letter, yet cannot find what the issue is. I have also reviewed the other similar questions on this problem, but nothing has helped - the project will not build at all.
MainActivity.cs:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace NewMapApp
{
[Activity (Label = "NewMapApp", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// this line causes the error
SetContentView (Resource.Layout.Main);
}
}
}
Main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
Stack trace:
Android.Views.InflateException: Binary XML file line #1: Error inflating class fragment
at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/9d03ce3e/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:895
at Android.App.Activity.SetContentView (int) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/9d03ce3e/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.App.Activity.cs:3830
at NewMapApp.MainActivity.OnCreate (Android.OS.Bundle) [0x0000e] in /Users/rorymccrossan/Documents/Projects/Source/Testing/NewMapApp/NewMapApp/MainActivity.cs:26
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/9d03ce3e/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.App.Activity.cs:1943
at at (wrapper dynamic-method) object.10990ff1-ed31-42fe-a0e5-3d5d23292191 (intptr,intptr,intptr) <IL 0x00017, 0x00043>
at
at --- End of managed exception stack trace ---
at android.view.InflateException: Binary XML file line #1: Error inflating class fragment
at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
at at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
at at android.app.Activity.setContentView(Activity.java:1956)
at at newmapapp.MainActivity.n_onCreate(Native Method)
at at newmapapp.MainActivity.onCreate(MainActivity.java:28)
at at android.app.Activity.performCreate(Activity.java:5372)
at at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at at android.app.ActivityThread.access$700(ActivityThread.java:159)
at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at at android.os.Handler.dispatchMessage(Handler.java:99)
at at android.os.Looper.loop(Looper.java:137)
at at android.app.ActivityThread.main(ActivityThread.java:5419)
at at java.lang.reflect.Method.invokeNative(Native Method)
at at java.lang.reflect.Method.invoke(Method.java:525)
at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at at dalvik.system.NativeStart.main(Native Method)
at Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
at at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
at at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
at at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
at ... 22 more
at
I am attempting to debug on a device (Samsung Galaxy S4) although it's not even getting that far when building.
As said above, I have followed all steps of the tutorial; added Google Play Services, all required permissions and now getting this. Can anyone guide me as to what the problem is?
I found the issue. The tutorial is missing an important setting in the AndroidManifest. As well as requiring certain permissions, you also need to provide the Google Play Services version as a meta-data node:
<application>
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
</application>
I have notified Xamarin of the problem by email, but will leave this question active in the hopes that it will help someone in the future.
It looks like you are not subclassing FragmentActivity, try:
// if you need to be compatible
using Android.Support.V4.App;
// if you don't need to be compatible
using Android.App;
public class MainActivity : FragmentActivity // <- You MUST be a FragmentActivity if you want to use fragments in your activity.
And you should no longer have that runtime issue.
Setting the AndroidManifest.xml file with the following permissions worked for me:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

ClassNotFoundException: com.google.android.gms.maps.MapFragment with AndroidStudio

I'm trying to use Google maps v2 on my android App. I'm using Android Studio; I followed steps indicated here: https://developers.google.com/maps/documentation/android/start#overview
In my AndroidManifest.xml I've:
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
On my build.gradle I've:
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 12
targetSdkVersion 16
}
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile 'com.google.android.gms:play-services:3.1.36'
}
My first Activity:
public class HomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
}
My layout:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment" />
I can compile my project but when I try to deploy to my phone I've this exception:
09-03 17:28:08.604 9787-9787/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{it.mobile/it.mobile.HomeActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.access$600(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:258)
at android.app.Activity.setContentView(Activity.java:1867)
at it.mobile.HomeActivity.onCreate(HomeActivity.java:12)
at android.app.Activity.performCreate(Activity.java:5020)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
... 11 more
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.Fragment.instantiate(Fragment.java:584)
at android.app.Fragment.instantiate(Fragment.java:552)
at android.app.Activity.onCreateView(Activity.java:4668)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
... 20 more
Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.Fragment.instantiate(Fragment.java:574)
... 23 more
In the properties of the project I check export to support library and google services library.
Thanks
At the end I solved the problem recreating a new project in Android Studio. Not sure which was the problem.

Android Google Maps V2 Error inflating fragment

I've seen dozens of similar problems but none have seemed to fix my issue. I have an app that uses google maps v2 (previously used v1). I imported google-play-services and android-support-v4, and created an activity that extended SupportFragment (also tried just fragment). I added all permissions and my key to the manifest, and created a fragment in my layout folder which defines the map. However, my app crashes every time I call setContentView on my fragment within my activity. I imported android.support.v4.app.FragmentActivity and android.support.v4.app.Fragment in this activity, but get an error trying to inflate the fragment. The logcat log is listed below. Thanks in advance!
public class ManualCheckInMapActivity extends android.support.v4.app.FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_fragment);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.safehouse.onguardianmobile/com.safehouse.onguardianmobile.ManualCheckInMapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:200)
at android.app.Activity.setContentView(Activity.java:1647)
at com.safehouse.onguardianmobile.ManualCheckInMapActivity.onCreate(ManualCheckInMapActivity.java:40)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)
... 20 more
Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.safehouse.onguardianmobile-2.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
... 23 more
UPDATE: I tried cleaning the project, but now get this error. I'm still new at this, no maybe I did not add the libraries properly?
Errors occurred during the build.
Errors running builder 'Android Package Builder' on project 'google-play-services_lib'.
Problems encountered while deleting resources.
Could not delete 'C:\Users\Travis\workspace\google-play-services_lib\bin\google-play-services_lib.jar'.
Problems encountered while deleting files.
Could not delete: C:\workspace\google-play-services_lib\bin\google-play-services_lib.jar.
Problems encountered while deleting resources.
Could not delete 'C:\workspace\google-play-services_lib\bin\google-play-services_lib.jar'.
Problems encountered while deleting files.
Could not delete: C:\Users\Travis\workspace\google-play-services_lib\bin\google-play-services_lib.jar.

"Error Inflating Class Fragment" on Android 4.2.x

I've been debugging for hours now and still no luck. I made a RSS Reader using Android fragments. It works amazingly well on devices running Android 4.0.x to 4.1.x but crashes on start on Android 4.2.x devices.
I'd really appreciate any help I can get.
Logcat:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.impsycho.androidpakistan/com.impsycho.androidpakistan.ItemListActivity}:
android.view.InflateException: Binary XML file line #1: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
at android.app.Activity.setContentView(Activity.java:1881)
at com.impsycho.androidpakistan.ItemListActivity.onCreate(ItemListActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
Caused by: java.lang.NullPointerException
at com.impsycho.androidpakistan.ItemListFragment$GetAllThePosts.onPreExecute(ItemListFragment.java:157)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at com.impsycho.androidpakistan.ItemListFragment.onCreate(ItemListFragment.java:54)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:835)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
... 20 more
Sending signal. PID: 30286 SIG: 9
Main Activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class ItemListActivity extends FragmentActivity implements ItemListFragment.Callbacks {
private boolean mTwoPane;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
getActionBar().setDisplayShowTitleEnabled(false);
if (findViewById(R.id.item_detail_container) != null) {
mTwoPane = true;
((ItemListFragment) getSupportFragmentManager()
.findFragmentById(R.id.item_list))
.setActivateOnItemClick(true);
}
}
...
activity_item_list.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/item_list"
android:name="com.impsycho.androidpakistan.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ItemListActivity" />
activity_item_twopane.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".ItemListActivity" >
<fragment
android:id="#+id/item_list"
android:name="com.impsycho.androidpakistan.ItemListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.5" />
<FrameLayout
android:id="#+id/item_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="5" />
</LinearLayout>
Okay, so I figured out what was wrong.
I was focusing on the wrong error. The error was actually in my method GetAllThePosts() which is called when the app is started, because of which the layout could not be inflated. And it's not even an error because it works perfectly well in all other Android versions.
For some reason, you can't change the view of menu items while the app is starting in Android 4.2.x
The code responsible was:
private class GetAllThePosts extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
RefreshMenuButton.setActionView(R.layout.action_progress);
RefreshMenuButton.expandActionView();
...
I just added a conditional statement to check if we were on Android 4.2 and higher. If we are it won't run the first time.

"Error inflating class fragment" with google map

I tried to make a sample project using Google Map, but I couldn't.
Help me please!
Test Device : Android 4.0.4
Error Message :
12-29 23:45:32.605: E/AndroidRuntime(9437): FATAL EXCEPTION: main
12-29 23:45:32.605: E/AndroidRuntime(9437):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.test_googlemap/com.example.test_googlemap.MainActivity}:
android.view.InflateException: Binary XML file line #2: Error
inflating class fragment 12-29 23:45:32.605: E/AndroidRuntime(9437):
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.app.ActivityThread.access$600(ActivityThread.java:127) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.os.Handler.dispatchMessage(Handler.java:99) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.os.Looper.loop(Looper.java:137) 12-29 23:45:32.605:
E/AndroidRuntime(9437): at
android.app.ActivityThread.main(ActivityThread.java:4507) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
java.lang.reflect.Method.invokeNative(Native Method) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
java.lang.reflect.Method.invoke(Method.java:511) 12-29 23:45:32.605:
E/AndroidRuntime(9437): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
dalvik.system.NativeStart.main(Native Method) 12-29 23:45:32.605:
E/AndroidRuntime(9437): Caused by: android.view.InflateException:
Binary XML file line #2: Error inflating class fragment 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.view.LayoutInflater.inflate(LayoutInflater.java:466) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.view.LayoutInflater.inflate(LayoutInflater.java:396) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.view.LayoutInflater.inflate(LayoutInflater.java:352) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:271)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.app.Activity.setContentView(Activity.java:1835) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
com.example.test_googlemap.MainActivity.onCreate(MainActivity.java:11)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.app.Activity.performCreate(Activity.java:4465) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
12-29 23:45:32.605: E/AndroidRuntime(9437): ... 11 more 12-29
23:45:32.605: E/AndroidRuntime(9437): Caused by:
java.lang.ClassCastException: com.google.android.gms.maps.MapFragment
cannot be cast to android.support.v4.app.Fragment 12-29 23:45:32.605:
E/AndroidRuntime(9437): at
android.support.v4.app.Fragment.instantiate(Fragment.java:394) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.support.v4.app.Fragment.instantiate(Fragment.java:369) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
12-29 23:45:32.605: E/AndroidRuntime(9437): ... 20 more
Source Code :
package com.example.test_googlemap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
Manifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_googlemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.test_googlemap.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="I removed it!"/>
</application>
<permission
android:name="com.example.Test_GoogleMap.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.Test_GoogleMap.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
</manifest>
You are extending FragmentActivity, indicating that you are trying to use the Android Support package backport of fragments. However, your <fragment> element refers to MapFragment, which is for the native API Level 11 edition of fragments.
Replace MapFragment with SupportMapFragment, and that should clear up this specific crash.
I had the same problem and I did the mistake to only add one of the 2 following tags.
Note that you are also missing one of these two
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="<YOUR VALUE>"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
The actual error is really misleading, as you might be thinking of some API level UI issue.
Some times you are using both -
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="<YOUR VALUE>"
/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="<YOUR VALUE>"
/>
Make sure , Don't use both ...
if you need Location , Places and maps then use geo.API_KEY
and if you need places and maps then use maps.v2.API_KEY
Accepted answer is correct but meaningful information i share with you, may be anyone facing same issue what i face
If everything is woking same as google code then please check manifest file in my case i added geo key and map key that's why exception occurs,
Note - do not add two keys in manifest file remove map key
meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/google_maps_key"/>
above code and add this code.
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/auto_location"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
This problem also occurs when it is not able to inflate the Fragment class mentioned in the activity_main.xml(or the XML file present under res/layout), due to a SDK version incompatibility in the andriod_manifest.xml file.
The correct versions for SDK levels to get the map on an emulator is this:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
Even if it's an already answered question this error can also appear if you call the super.onCreateViewin your fragment. It will crash at run time.
Be sure you overridden the onCreateView method and inflated your layout:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
return view;
}
In my case, I had to do two corrections to make this exception go away.
The activity should extend FragmentActivity and not Activity
Manifest file needs a uses-permission for ACCESS_NETWORK_STATE (my file already had INTERNET permission)
This defect has been resolved in play services library v9.0.0. https://code.google.com/p/gmaps-api-issues/issues/detail?id=9021#makechanges
Use the internet permission to be direct child of manifest file.. Like below and try..
Also You should have the following for using map:
1.Should extend Map activity in your activity file
2.Should have API key i didn't see any key in your code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_googlemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="15" />
Do u have valid Google Map API key?
try using
public class MainActivity extends MapActivity{
... }

Categories

Resources