无法实例化片段com.google.android.gms.MapFragment

时间:2015-03-01 08:31:49

标签: android google-maps android-fragments fragment

我知道这个问题被问了很多次。但在我的情况下不工作尝试了这么多解决方案。请帮忙

我的mainActivity扩展onCreate地图中的Activity以这种方式生成。

private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager()
                .findFragmentById(R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

和xml视图是

    <fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

支持:

    <uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />

错误:

    03-01 13:29:20.089: E/AndroidRuntime(26406): FATAL EXCEPTION: main
03-01 13:29:20.089: E/AndroidRuntime(26406): Process: com.commteldigital.cinecorn.activity, PID: 26406
03-01 13:29:20.089: E/AndroidRuntime(26406): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.commteldigital.cinecorn.activity/com.commteldigital.cmovies.activity.LocationActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.ActivityThread.access$800(ActivityThread.java:151)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.os.Handler.dispatchMessage(Handler.java:110)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.os.Looper.loop(Looper.java:193)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.ActivityThread.main(ActivityThread.java:5299)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at java.lang.reflect.Method.invokeNative(Native Method)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at java.lang.reflect.Method.invoke(Method.java:515)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at dalvik.system.NativeStart.main(Native Method)
03-01 13:29:20.089: E/AndroidRuntime(26406): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:305)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.Activity.setContentView(Activity.java:1944)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at com.commteldigital.cmovies.activity.LocationActivity.onCreate(LocationActivity.java:27)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.Activity.performCreate(Activity.java:5264)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
03-01 13:29:20.089: E/AndroidRuntime(26406):    ... 11 more
03-01 13:29:20.089: E/AndroidRuntime(26406): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.Fragment.instantiate(Fragment.java:597)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.Fragment.instantiate(Fragment.java:561)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.Activity.onCreateView(Activity.java:4811)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
03-01 13:29:20.089: E/AndroidRuntime(26406):    ... 21 more
03-01 13:29:20.089: E/AndroidRuntime(26406): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.MapFragment" on path: DexPathList[[zip file "/data/app/com.commteldigital.cinecorn.activity-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.commteldigital.cinecorn.activity-2, /vendor/lib, /system/lib]]
03-01 13:29:20.089: E/AndroidRuntime(26406):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
03-01 13:29:20.089: E/AndroidRuntime(26406):    at android.app.Fragment.instantiate(Fragment.java:583)
03-01 13:29:20.089: E/AndroidRuntime(26406):    ... 24 more

好。从extras文件夹导入了google play lib。为什么班级不可用。提前谢谢

3 个答案:

答案 0 :(得分:2)

您似乎使用了错误的名称。使用此代码:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.MapFragment"/>
</merge>

此外,getMap()现已弃用,您使用的是旧API。

答案 1 :(得分:1)

更改

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<fragment
    android:id="@+id/map"
    class="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

答案 2 :(得分:0)

如果您没有添加这些行,请在application标记内的清单文件中添加这些行

<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />