我提出了以下错误:
com.example.thejdeep.healthpal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.thejdeep.healthpal, PID: 21085
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thejdeep.healthpal/com.example.thejdeep.healthpal.Hospitals}: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
Caused by: java.lang.RuntimeException: API key not found. Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
at com.google.maps.api.android.lib6.drd.r.b(:com.google.android.gms.DynamiteModulesB:115)
at com.google.maps.api.android.lib6.auth.e.a(:com.google.android.gms.DynamiteModulesB:155)
at com.google.android.gms.maps.internal.CreatorImpl.b(:com.google.android.gms.DynamiteModulesB:6074)
at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms.DynamiteModulesB:72)
at com.google.android.gms.maps.internal.i.onTransact(:com.google.android.gms.DynamiteModulesB:62)
at android.os.Binder.transact(Binder.java:499)
at uqw.newMapFragmentDelegate(:com.google.android.gms:179)
at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms:99)
at uqv.onTransact(:com.google.android.gms:62)
at android.os.Binder.transact(Binder.java:499)
at com.google.android.gms.maps.internal.zzc$zza$zza.zzI(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$zzb.zzJy(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$zzb.zza(Unknown Source)
at com.google.android.gms.dynamic.zza.zza(Unknown Source)
at com.google.android.gms.dynamic.zza.onInflate(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
at android.support.v4.app.Fragment.onInflate(Fragment.java:1184)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3412)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:378)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412)
at android.app.Activity.setContentView(Activity.java:2414)
at com.example.thejdeep.healthpal.Hospitals.onCreate(Hospitals.java:23)
at android.app.Activity.performCreate(Activity.java:6682)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
02-19 14:27:36.110 21085-21085/com.example.thejdeep.healthpal E/AndroidRuntime:
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这是我的源代码:
public class Hospitals extends FragmentActivity implements OnMapReadyCallback{
private GoogleMap mMap;
protected void onCreate(Bundle savedInstanceState ){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hospitals);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
public void onMapReady(GoogleMap googleMap){
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
这是我的XML文件(R.layout.activity_hospitals):
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.thejdeep.healthpal.Hospitals" />
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thejdeep.healthpal">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Home"></activity>
<activity android:name=".Hospitals"></activity>
<activity
android:name=".Restaurants"
android:label="Map">
</activity>
<activity android:name=".ActivityTracker"></activity>
</application>
</manifest>
我试图将android:name更改为类,但它不起作用。 我将API密钥设置为字符串值。
有人可以指出此代码中出现了什么问题吗?
答案 0 :(得分:0)
在:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
尝试使用实际的API_KEY字符串值而不是@ string / reference。
如果一切正常,您可以尝试将此键移动到xml并提供参考,如下所示:https://stackoverflow.com/a/30877372/1533933