嗨,我在使用 google maps v2 api 时遇到了这个问题,我遵循了许多教程(也是官方教程),而且我也看到了很多解决此错误的方法,但它们不起作用。如果我在我的设备(acer e330)上运行应用程序,我有这个 logcat 错误:
06-23 21:11:08.391: E/AndroidRuntime(9419): FATAL EXCEPTION: main
06-23 21:11:08.391: E/AndroidRuntime(9419): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mappa2/com.example.mappa2.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.os.Handler.dispatchMessage(Handler.java:99)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.os.Looper.loop(Looper.java:137)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-23 21:11:08.391: E/AndroidRuntime(9419): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 21:11:08.391: E/AndroidRuntime(9419): at java.lang.reflect.Method.invoke(Method.java:511)
06-23 21:11:08.391: E/AndroidRuntime(9419): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
06-23 21:11:08.391: E/AndroidRuntime(9419): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
06-23 21:11:08.391: E/AndroidRuntime(9419): at dalvik.system.NativeStart.main(Native Method)
06-23 21:11:08.391: E/AndroidRuntime(9419): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-23 21:11:08.391: E/AndroidRuntime(9419): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.app.Activity.setContentView(Activity.java:1835)
06-23 21:11:08.391: E/AndroidRuntime(9419): at com.example.mappa2.MainActivity.onCreate(MainActivity.java:23)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.app.Activity.performCreate(Activity.java:4465)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-23 21:11:08.391: E/AndroidRuntime(9419): ... 11 more
06-23 21:11:08.391: E/AndroidRuntime(9419): 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
06-23 21:11:08.391: E/AndroidRuntime(9419): at maps.aj.ay.a(Unknown Source)
06-23 21:11:08.391: E/AndroidRuntime(9419): at maps.aj.ay.a(Unknown Source)
06-23 21:11:08.391: E/AndroidRuntime(9419): at maps.aj.al.a(Unknown Source)
06-23 21:11:08.391: E/AndroidRuntime(9419): at maps.aj.bf.a(Unknown Source)
06-23 21:11:08.391: E/AndroidRuntime(9419): at maps.aj.be.a(Unknown Source)
06-23 21:11:08.391: E/AndroidRuntime(9419): at bcw.onTransact(SourceFile:107)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.os.Binder.transact(Binder.java:297)
06-23 21:11:08.391: E/AndroidRuntime(9419): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
06-23 21:11:08.391: E/AndroidRuntime(9419): at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
06-23 21:11:08.391: E/AndroidRuntime(9419): at com.google.android.gms.internal.bb$4.b(Unknown Source)
06-23 21:11:08.391: E/AndroidRuntime(9419): at com.google.android.gms.internal.bb.a(Unknown Source)
06-23 21:11:08.391: E/AndroidRuntime(9419): at com.google.android.gms.internal.bb.onCreateView(Unknown Source)
06-23 21:11:08.391: E/AndroidRuntime(9419): at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
06-23 21:11:08.391: E/AndroidRuntime(9419): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
06-23 21:11:08.391: E/AndroidRuntime(9419): ... 21 more
这是我的MainActivity.java
package com.example.mappa2;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
private GoogleMap map;
public LatLng casa=new LatLng(42.93289301245662,13.085575103759766);
public LatLng piazza=new LatLng(42.931344,13.087301);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
crea();
}
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void crea(){
if(map==null){
map=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Marker c=map.addMarker(new MarkerOptions().position(casa).title("Casa"));
Marker d=map.addMarker(new MarkerOptions().position(piazza).title("Piazza"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(casa, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(10),2000,null);
}
}
}
显现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mappa2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<permission
android:name="com.example.mappa2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.mappa2.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-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mappa2.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.API_KEY"
android:value="AIzaSyBgYv8RmGt2xuHR3LL9JaNQVulvEW2C3P0" />
</application>
</manifest>