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.onCreateView
in 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{ ... }
来源:https://stackoverflow.com/questions/14082851/error-inflating-class-fragment-with-google-map