Android Error: Binary XML file line #16: Error inflating class fragment when adding map fragment

一个人想着一个人 提交于 2019-12-12 18:33:55

问题


I'm trying to display map using map api v2.

Here is my xml code:

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

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

Here is Code in activity:

 import com.google.android.gms.maps.GoogleMap;
 import com.google.android.gms.maps.MapFragment;


 public class MainActivity extends Activity  {

 private GoogleMap map;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

I'm trying to run it on my device (Andorid 4.1.2 which is api 16) I've added google-play-services-lib in my procject. When I run app, it just crashes.

Here is full logcat log:

07-23 12:55:30.955: E/AndroidRuntime(14098): FATAL EXCEPTION: main
07-23 12:55:30.955: E/AndroidRuntime(14098): java.lang.RuntimeException: Unable to start activity ComponentInfo{stringbind.nikolacjenolomacdemo/stringbind.nikolacjenolomacdemo.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.os.Looper.loop(Looper.java:137)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.ActivityThread.main(ActivityThread.java:4898)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at java.lang.reflect.Method.invokeNative(Native Method)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at java.lang.reflect.Method.invoke(Method.java:511)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at dalvik.system.NativeStart.main(Native Method)
07-23 12:55:30.955: E/AndroidRuntime(14098): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.Activity.setContentView(Activity.java:1924)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at stringbind.nikolacjenolomacdemo.MainActivity.onCreate(MainActivity.java:18)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.Activity.performCreate(Activity.java:5206)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-23 12:55:30.955: E/AndroidRuntime(14098):    ... 11 more
07-23 12:55:30.955: E/AndroidRuntime(14098): Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
07-23 12:55:30.955: E/AndroidRuntime(14098):    at maps.ah.ba.a(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at maps.ah.ba.a(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at maps.ah.an.a(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at maps.ah.bh.a(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at maps.ah.bg.a(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at bgl.onTransact(SourceFile:107)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.os.Binder.transact(Binder.java:326)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at com.google.android.gms.maps.MapFragment$a.onCreateView(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at com.google.android.gms.internal.bb$4.b(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at com.google.android.gms.internal.bb.a(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at com.google.android.gms.internal.bb.onCreateView(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:807)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1112)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.app.Activity.onCreateView(Activity.java:4857)
07-23 12:55:30.955: E/AndroidRuntime(14098):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
07-23 12:55:30.955: E/AndroidRuntime(14098):    ... 21 more
07-23 12:55:30.985: D/dalvikvm(14098): GC_CONCURRENT freed 285K, 7% free 12559K/13447K, paused 12ms+2ms, total 33ms

UPDATE:

Here's manifest file:

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="stringbind.nikolacjenolomacdemo"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="12"
            android:targetSdkVersion="17" />

        <uses-permission android:name="com.vogella.android.locationapi.maps.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" />

        <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="**MY KEY**"/>

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="stringbind.nikolacjenolomacdemo.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>
        </application>

    </manifest>

回答1:


Your meta tag should be under application tag

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
   ...// rest of the code
   <meta-data
     android:name="com.google.android.maps.v2.API_KEY"
     android:value="**MY KEY**"/>
 </application>

Check the docs under the topic Adding the API Key to your application

https://developers.google.com/maps/documentation/android/start

Edit:

Your also missing

  <permission
    android:name="stringbind.nikolacjenolomacdemo.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>

Also use your package name

   <uses-permission android:name="stringbind.nikolacjenolomacdemo.permission.MAPS_RECEIVE" />

Also missing

    <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>


来源:https://stackoverflow.com/questions/17808545/android-error-binary-xml-file-line-16-error-inflating-class-fragment-when-add

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!