我知道之前已经问过这个问题,但我还没有找到解决方案,我写了一个Android应用程序,但它没有工作,我不知道问题是什么,我几乎尝试任何事情所以请帮助!!
这是我的代码:
MainActivity java: -
package com.startPoint.gmap;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
清单: -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.startPoint.gmap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<permission android:name="com.startPoint.gmap.MAPS_RECEIVE" android:protectionLevel="signature"></permission>
<uses-permission android:name="com.startPoint.gmap.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.startPoint.gmap.providers.gsf.permissions.READ_GSEREVICES"/>
<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.startPoint.gmap.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.v2.API_KEY"
android:value="API KEY HERE"/>
</application>
activity_main xml: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.startPoint.gmap.MainActivity$PlaceholderFragment" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
,这是我得到的错误: -
03-24 03:25:13.935: D/AndroidRuntime(12021): Shutting down VM
03-24 03:25:13.935: W/dalvikvm(12021): threadid=1: thread exiting with uncaught exception (group=0x41cda700)
03-24 03:25:13.940: E/AndroidRuntime(12021): FATAL EXCEPTION: main
03-24 03:25:13.940: E/AndroidRuntime(12021): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.startPoint.gmap/com.startPoint.gmap.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.app.ActivityThread.access$700(ActivityThread.java:159)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.os.Looper.loop(Looper.java:176)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.app.ActivityThread.main(ActivityThread.java:5419)
03-24 03:25:13.940: E/AndroidRuntime(12021): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 03:25:13.940: E/AndroidRuntime(12021): at java.lang.reflect.Method.invoke(Method.java:525)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
03-24 03:25:13.940: E/AndroidRuntime(12021): at dalvik.system.NativeStart.main(Native Method)
03-24 03:25:13.940: E/AndroidRuntime(12021): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.app.Activity.setContentView(Activity.java:1956)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.startPoint.gmap.MainActivity.onCreate(MainActivity.java:17)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.app.Activity.performCreate(Activity.java:5372)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
03-24 03:25:13.940: E/AndroidRuntime(12021): ... 11 more
03-24 03:25:13.940: E/AndroidRuntime(12021): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4030500 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.google.android.gms.maps.internal.q.v(Unknown Source)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.google.android.gms.maps.internal.q.u(Unknown Source)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.google.android.gms.maps.SupportMapFragment$b.cE(Unknown Source)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.google.android.gms.dynamic.a.a(Unknown Source)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
03-24 03:25:13.940: E/AndroidRuntime(12021): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
03-24 03:25:13.940: E/AndroidRuntime(12021): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
03-24 03:25:13.940: E/AndroidRuntime(12021): ... 21 more
我在 CatLog 中发现我应该在最明显的地方添加以下权限
<meta-data android:name="com.google.android.gms.version"
所以我做了但是我仍然有一个错误,但CatLog不同是否是同样的老问题?
新版LogCat
04-07 20:05:37.830: I/Google Maps Android API(26532): Google Play services client version: 4030500
04-07 20:05:37.880: I/Google Maps Android API(26532): Google Play services package version: 4324036
04-07 20:05:38.825: D/dalvikvm(26532): GC_FOR_ALLOC freed 554K, 15% free 9888K/11632K, paused 42ms, total 43ms
04-07 20:05:38.930: D/dalvikvm(26532): GC_FOR_ALLOC freed 191K, 15% free 9944K/11632K, paused 17ms, total 17ms
04-07 20:05:38.930: I/dalvikvm-heap(26532): Grow heap (frag case) to 10.884MB for 18832-byte allocation
04-07 20:05:38.950: D/dalvikvm(26532): GC_FOR_ALLOC freed 0K, 15% free 9962K/11652K, paused 18ms, total 18ms
04-07 20:05:39.170: D/dalvikvm(26532): GC_FOR_ALLOC freed 69K, 14% free 10238K/11880K, paused 30ms, total 30ms
04-07 20:05:39.720: D/AbsListView(26532): Get MotionRecognitionManager
04-07 20:05:39.755: D/AbsListView(26532): onVisibilityChanged() is called, visibility : 8
04-07 20:05:39.755: D/AbsListView(26532): unregisterIRListener() is called
04-07 20:05:39.760: D/AbsListView(26532): onVisibilityChanged() is called, visibility : 8
04-07 20:05:39.760: D/AbsListView(26532): unregisterIRListener() is called
04-07 20:05:39.760: D/AbsListView(26532): onVisibilityChanged() is called, visibility : 0
04-07 20:05:39.760: D/AbsListView(26532): unregisterIRListener() is called
04-07 20:05:39.970: D/AndroidRuntime(26532): Shutting down VM
04-07 20:05:39.970: W/dalvikvm(26532): threadid=1: thread exiting with uncaught exception (group=0x41c21700)
04-07 20:05:40.000: E/AndroidRuntime(26532): FATAL EXCEPTION: main
04-07 20:05:40.000: E/AndroidRuntime(26532): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.startPoint.gmap/com.startPoint.gmap.MainActivity}: java.lang.NullPointerException
04-07 20:05:40.000: E/AndroidRuntime(26532): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-07 20:05:40.000: E/AndroidRuntime(26532): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-07 20:05:40.000: E/AndroidRuntime(26532): at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-07 20:05:40.000: E/AndroidRuntime(26532): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-07 20:05:40.000: E/AndroidRuntime(26532): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 20:05:40.000: E/AndroidRuntime(26532): at android.os.Looper.loop(Looper.java:176)
04-07 20:05:40.000: E/AndroidRuntime(26532): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-07 20:05:40.000: E/AndroidRuntime(26532): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 20:05:40.000: E/AndroidRuntime(26532): at java.lang.reflect.Method.invoke(Method.java:525)
04-07 20:05:40.000: E/AndroidRuntime(26532): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
04-07 20:05:40.000: E/AndroidRuntime(26532): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-07 20:05:40.000: E/AndroidRuntime(26532): at dalvik.system.NativeStart.main(Native Method)
04-07 20:05:40.000: E/AndroidRuntime(26532): Caused by: java.lang.NullPointerException
04-07 20:05:40.000: E/AndroidRuntime(26532): at com.startPoint.gmap.MainActivity.onCreate(MainActivity.java:20)
04-07 20:05:40.000: E/AndroidRuntime(26532): at android.app.Activity.performCreate(Activity.java:5372)
04-07 20:05:40.000: E/AndroidRuntime(26532): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-07 20:05:40.000: E/AndroidRuntime(26532): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
04-07 20:05:40.000: E/AndroidRuntime(26532): ... 11 more
答案 0 :(得分:0)
您需要将以下内容添加到<application>
标记下的清单文件中:
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
修改强> 假设您正确设置了Google Play服务,则可以在加载活动时运行检查以查看它是否为accessibe。
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (errorCode == ConnectionResult.SUCCESS) {
// ** GO TO YOUR MAP HERE
FragmentTransaction transaction = getFragmentManager().beginTransaction();
MyFragment myMapFragment = new MyFragment();
transaction.replace(R.id.your_fragment_holder, myMapFragment, "MAP").commit();
} else {
Dialog theDialog = GooglePlayServicesUtil.getErrorDialog(errorCode, this, 1);
theDialog.setTitle("Map update required");
theDialog.setCancelable(true);
theDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//** HANDLE WHEN USER HAS NO MAPS AVAILABLE
FragmentTransaction transaction = getFragmentManager().beginTransaction();
NonMapFragment myNonMapFragment = new NonMapFragment();
transaction.replace(R.id.my_fragment_manager, myNonMapFragment, "NOTAMAP").commit();
}
});
theDialog.show();
}
这至少会检查设备是否能够使用地图。
答案 1 :(得分:0)
此处您的最低SDK版本为18.因此您需要使用
android:name="com.google.android.gms.maps.MapFragment"
而不是
android:name="com.google.android.gms.maps.SupportMapFragment"
在片段的xml文件中。
之后,您的logcat清楚地表明您没有在&lt;之间设置 google_play_services_version 。应用程序&gt;标签。所以你需要使用它。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.startPoint.gmap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<permission android:name="com.startPoint.gmap.MAPS_RECEIVE" android:protectionLevel="signature"></permission>
<uses-permission android:name="com.startPoint.gmap.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.startPoint.gmap.providers.gsf.permissions.READ_GSEREVICES"/>
<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.startPoint.gmap.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.v2.API_KEY"
android:value="API KEY HERE"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
此处也从
更改 map=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
到
map=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();