您好我想在我的Android片段中显示谷歌地图。我做的一切都很好,但我不知道这个错误来自哪里。我试图改变不同的东西,但根本不适用于我,下面是代码。
package com.example.nadeemahmad.guitest;
import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
public class profile extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap googleMap;
ImageButton pr_img_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
//AIzaSyAjxQKEZc9MHTH3f173Iv21Gw2c6IeUX2U
pr_img_btn = (ImageButton) findViewById(R.id.pr_img_btn);
pr_img_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(profile.this,settings.class);
startActivity(i);
}
});
if(googleServicesAvailble()){
Toast.makeText(profile.this,"Perfect!!!",Toast.LENGTH_LONG).show();
initMap();
}
}
private void initMap() {
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapFrag);
mapFragment.getMapAsync(this);
}
public boolean googleServicesAvailble(){
GoogleApiAvailability gpa = GoogleApiAvailability.getInstance();
int avail = gpa.isGooglePlayServicesAvailable(this);
if(avail == ConnectionResult.SUCCESS){
return true;
} else if(gpa.isUserResolvableError(avail)){
Dialog dialog = gpa.getErrorDialog(this,avail,0);
dialog.show();
} else{
Toast.makeText(profile.this,"Can't connect to play store",Toast.LENGTH_LONG).show();
}
return false;
}
@Override
public void onMapReady(GoogleMap Map) {
googleMap = Map;
}
}
以下是权限的清单文件。还有元数据和元数据。一切都很好,没有任何错误。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nadeemahmad.guitest">
<!--Permissions-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission android:name="com.example.nadeemahmad.guitest.MAPS.RECIEVE" android:protectionLevel="signature"/>
<uses-permission android:name="com.example.nadeemahmad.guitest.MAPS.RECIEVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permissions.READ_GSERVICES"/>
<uses-feature android:glEsVersion="0x0020000" android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".splash"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:noHistory="true" />
<activity
android:name=".profile"
android:noHistory="true" />
<activity
android:name=".profile_update"
android:noHistory="true" />
<activity
android:name=".sync_contacts"
android:noHistory="true" />
<activity
android:name=".settings"
android:noHistory="true" />
<activity android:name=".synced_contacts_list"></activity>
</application>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDKymeBXNeiFWY5jRUejv6zItpmr2MVyQ0" />
<meta-data android:name="come.google.gms.version" android:version="8487000"/>
</manifest>
这是向我展示的例外情况!
07-11 10:05:20.044 12147-12147/com.example.nadeemahmad.guitest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nadeemahmad.guitest, PID: 12147
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nadeemahmad.guitest/com.example.nadeemahmad.guitest.profile}: android.view.InflateException: Binary XML file line #70: Binary XML file line #70: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: android.view.InflateException: Binary XML file line #70: Binary XML file line #70: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #70: 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.q.b(:com.google.android.gms.DynamiteModulesB:39)
at com.google.maps.api.android.lib6.auth.e.a(:com.google.android.gms.DynamiteModulesB:12)
at com.google.android.gms.maps.internal.CreatorImpl.a(:com.google.android.gms.DynamiteModulesB:121)
at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms.DynamiteModulesB:25)
at com.google.android.gms.maps.internal.g.onTransact(:com.google.android.gms.DynamiteModulesB:35)
at android.os.Binder.transact(Binder.java:507)
at uni.newMapFragmentDelegate(:com.google.android.gms:1)
at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms:1)
at unh.onTransact(:com.google.android.gms:13)
at android.os.Binder.transact(Binder.java:507)
at com.google.android.gms.internal.zzed.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzf.zzH(Unknown Source)
at com.google.android.gms.maps.MapFragment$zzb.zzwg(Unknown Source)
at com.google.android.gms.maps.MapFragment$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.MapFragment.onInflate(Unknown Source)
at android.app.Fragment.onInflate(Fragment.java:1419)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2314)
at android.app.FragmentController.onCreateView(FragmentController.java:99)
at android.app.Activity.onCreateView(Activity.java:6080)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:784)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:873)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.nadeemahmad.guitest.profile.onCreate(profile.java:27)
07-11 10:05:20.044 12147-12147/com.example.nadeemahmad.guitest E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6942)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
答案 0 :(得分:0)
如果您阅读错误,您就会知道 引起:java.lang.RuntimeException:找不到API密钥。检查它是否在AndroidManifest.xml的元素中
请将com.google.android.maps.v2.API_KEY更改为清单中的com.google.android.geo.API_KEY并尝试。
答案 1 :(得分:0)
正如我在您的日志中看到的那样,您错过了API密钥
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
将此行放在应用程序标记
中的元数据中
<application
...
...>
...
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDKymeBXNeiFWY5jRUejv6zItpmr2MVyQ0" />
<meta-data android:name="come.google.gms.version"
android:version="8487000"/>
...
</application>