使用eclipse。
我想在我的应用上使用谷歌地图。 当我在genymotion模拟器上运行app logcut show error:android.view.InflateException:二进制XML文件行#45:错误膨胀类片段。
我在eclipse中添加google-play-services_lib并将其添加到库中。
MainActivity:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) {
setContentView(R.layout.activity_map);
if (initMap()) {
// mMap.setMyLocationEnabled(true);
mLocationClient = new LocationClient(this, this, this);
mLocationClient.connect();
}
else {
Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show();
}
}
else {
setContentView(R.layout.activity_main);
}
}
}
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gmapsapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />
<permission
android:name="com.example.gmapsapp.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gmapsapp.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-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.gmapsapp.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.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
activity_map:
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
logcat:
12-14 23:09:15.781: E/AndroidRuntime(29819): FATAL EXCEPTION: main
12-14 23:09:15.781: E/AndroidRuntime(29819): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gmapsapp/com.example.gmapsapp.MainActivity}: android.view.InflateException: Binary XML file line #45: Error inflating class fragment
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.app.ActivityThread.access$700(ActivityThread.java:157)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.os.Looper.loop(Looper.java:176)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.app.ActivityThread.main(ActivityThread.java:5319)
12-14 23:09:15.781: E/AndroidRuntime(29819): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 23:09:15.781: E/AndroidRuntime(29819): at java.lang.reflect.Method.invoke(Method.java:511)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-14 23:09:15.781: E/AndroidRuntime(29819): at dalvik.system.NativeStart.main(Native Method)
12-14 23:09:15.781: E/AndroidRuntime(29819): Caused by: android.view.InflateException: Binary XML file line #45: Error inflating class fragment
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.app.Activity.setContentView(Activity.java:1932)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.example.gmapsapp.MainActivity.onCreate(MainActivity.java:61)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.app.Activity.performCreate(Activity.java:5326)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
12-14 23:09:15.781: E/AndroidRuntime(29819): ... 11 more
12-14 23:09:15.781: E/AndroidRuntime(29819): 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
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.google.maps.api.android.lib6.gmm6.c.g.a(Unknown Source)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.google.maps.api.android.lib6.c.i.a(Unknown Source)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.google.maps.api.android.lib6.c.ab.a(Unknown Source)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.google.maps.api.android.lib6.c.aa.a(Unknown Source)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.google.android.gms.maps.internal.x.onTransact(SourceFile:107)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.os.Binder.transact(Binder.java:310)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.google.android.gms.dynamic.a$4.b(Unknown Source)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.google.android.gms.dynamic.a.a(Unknown Source)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
12-14 23:09:15.781: E/AndroidRuntime(29819): at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
12-14 23:09:15.781: E/AndroidRuntime(29819): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)
12-14 23:09:15.781: E/AndroidRuntime(29819): ... 21 more
答案 0 :(得分:2)
E/AndroidRuntime(29819): 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
如错误所示,您的清单缺少API密钥的<meta-data>
元素:
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="Please type in your API key here, replacing this stub value"/>