我正在尝试放入我的应用程序 Google 地图。我下载了官方示例,但由于Error inflating class fragmemt in binary xml
. 我在我的应用程序中放置了一个示例活动,但它也因此而崩溃。我查看了许多类似的 Stackoverflow 问题,但没有找到答案。
我的代码:
BasicMapActivity.java:
package com.reportme.ReportMe;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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.MarkerOptions;
public class BasicMapActivity extends FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title(
"Marker"));
}
}
basic_demo.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.SupportMapFragment"/>
日志猫:
08-08 21:44:13.648: E/AndroidRuntime(24223): FATAL EXCEPTION: main
08-08 21:44:13.648: E/AndroidRuntime(24223): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.reportme.ReportMe/com.reportme.ReportMe.BasicMapActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.app.ActivityThread.access$600(ActivityThread.java:151)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.os.Looper.loop(Looper.java:155)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.app.ActivityThread.main(ActivityThread.java:5511)
08-08 21:44:13.648: E/AndroidRuntime(24223): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 21:44:13.648: E/AndroidRuntime(24223): at java.lang.reflect.Method.invoke(Method.java:511)
08-08 21:44:13.648: E/AndroidRuntime(24223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
08-08 21:44:13.648: E/AndroidRuntime(24223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
08-08 21:44:13.648: E/AndroidRuntime(24223): at dalvik.system.NativeStart.main(Native Method)
08-08 21:44:13.648: E/AndroidRuntime(24223): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-08 21:44:13.648: E/AndroidRuntime(24223): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:369)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.app.Activity.setContentView(Activity.java:1912)
08-08 21:44:13.648: E/AndroidRuntime(24223): at com.reportme.ReportMe.BasicMapActivity.onCreate(BasicMapActivity.java:44)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.app.Activity.performCreate(Activity.java:5066)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
08-08 21:44:13.648: E/AndroidRuntime(24223): ... 11 more
08-08 21:44:13.648: E/AndroidRuntime(24223): 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
08-08 21:44:13.648: E/AndroidRuntime(24223): at maps.ag.bb.a(Unknown Source)
08-08 21:44:13.648: E/AndroidRuntime(24223): at maps.ag.bb.a(Unknown Source)
08-08 21:44:13.648: E/AndroidRuntime(24223): at maps.ag.an.a(Unknown Source)
08-08 21:44:13.648: E/AndroidRuntime(24223): at maps.ag.bi.a(Unknown Source)
08-08 21:44:13.648: E/AndroidRuntime(24223): at maps.ag.bh.a(Unknown Source)
08-08 21:44:13.648: E/AndroidRuntime(24223): at blf.onTransact(SourceFile:107)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.os.Binder.transact(Binder.java:326)
08-08 21:44:13.648: E/AndroidRuntime(24223): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
08-08 21:44:13.648: E/AndroidRuntime(24223): at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
08-08 21:44:13.648: E/AndroidRuntime(24223): at com.google.android.gms.internal.bb$4.b(Unknown Source)
08-08 21:44:13.648: E/AndroidRuntime(24223): at com.google.android.gms.internal.bb.a(Unknown Source)
08-08 21:44:13.648: E/AndroidRuntime(24223): at com.google.android.gms.internal.bb.onCreateView(Unknown Source)
08-08 21:44:13.648: E/AndroidRuntime(24223): at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
08-08 21:44:13.648: E/AndroidRuntime(24223): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
08-08 21:44:13.648: E/AndroidRuntime(24223): ... 20 more
我感谢任何答案。
波阿斯