在我更新sdk管理器中的某些项目之前,一切正常。现在,每当我尝试运行具有谷歌地图的应用程序时,我的应用程序崩溃了。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.prototype"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<permission
android:name="test.prototype.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="test.prototype.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET" />
<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="test.prototype.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>
<activity android:name="test.prototype.MapDetail" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="myApiKey" />
<uses-library android:name="com.google.android.maps" />
</application>
</manifest>
MainActivity :
public class MainActivity extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
TextView coordinates = (TextView) findViewById(R.id.coordinates);
coordinates.setText("Latitude: " + latitude + "\n" + "Longitude: "+ longitude );
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/coordinates"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/coordinates" />
</RelativeLayout>
好的,所以我添加了
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
清单中的。清单更新。
但仍有错误:
02-25 05:18:31.026: E/AndroidRuntime(6265): FATAL EXCEPTION: main
02-25 05:18:31.026: E/AndroidRuntime(6265): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.prototype/test.prototype.MainActivity}: java.lang.NullPointerException
02-25 05:18:31.026: E/AndroidRuntime(6265): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
02-25 05:18:31.026: E/AndroidRuntime(6265): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
02-25 05:18:31.026: E/AndroidRuntime(6265): at android.app.ActivityThread.access$600(ActivityThread.java:162)
02-25 05:18:31.026: E/AndroidRuntime(6265): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
02-25 05:18:31.026: E/AndroidRuntime(6265): at android.os.Handler.dispatchMessage(Handler.java:107)
02-25 05:18:31.026: E/AndroidRuntime(6265): at android.os.Looper.loop(Looper.java:194)
02-25 05:18:31.026: E/AndroidRuntime(6265): at android.app.ActivityThread.main(ActivityThread.java:5371)
02-25 05:18:31.026: E/AndroidRuntime(6265): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 05:18:31.026: E/AndroidRuntime(6265): at java.lang.reflect.Method.invoke(Method.java:525)
02-25 05:18:31.026: E/AndroidRuntime(6265): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
02-25 05:18:31.026: E/AndroidRuntime(6265): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-25 05:18:31.026: E/AndroidRuntime(6265): at dalvik.system.NativeStart.main(Native Method)
02-25 05:18:31.026: E/AndroidRuntime(6265): Caused by: java.lang.NullPointerException
02-25 05:18:31.026: E/AndroidRuntime(6265): at test.prototype.MainActivity.onCreate(MainActivity.java:28)
02-25 05:18:31.026: E/AndroidRuntime(6265): at android.app.Activity.performCreate(Activity.java:5122)
02-25 05:18:31.026: E/AndroidRuntime(6265): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
02-25 05:18:31.026: E/AndroidRuntime(6265): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
02-25 05:18:31.026: E/AndroidRuntime(6265): ... 11 more
在其他设备上运行后,LogCat 已更新:
02-25 05:38:40.462: E/AndroidRuntime(4342): FATAL EXCEPTION: main
02-25 05:38:40.462: E/AndroidRuntime(4342): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.prototype/test.prototype.MainActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2372)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.app.ActivityThread.access$600(ActivityThread.java:162)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.os.Handler.dispatchMessage(Handler.java:107)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.os.Looper.loop(Looper.java:194)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.app.ActivityThread.main(ActivityThread.java:5400)
02-25 05:38:40.462: E/AndroidRuntime(4342): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 05:38:40.462: E/AndroidRuntime(4342): at java.lang.reflect.Method.invoke(Method.java:525)
02-25 05:38:40.462: E/AndroidRuntime(4342): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837)
02-25 05:38:40.462: E/AndroidRuntime(4342): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
02-25 05:38:40.462: E/AndroidRuntime(4342): at dalvik.system.NativeStart.main(Native Method)
02-25 05:38:40.462: E/AndroidRuntime(4342): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-25 05:38:40.462: E/AndroidRuntime(4342): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:297)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.app.Activity.setContentView(Activity.java:1881)
02-25 05:38:40.462: E/AndroidRuntime(4342): at test.prototype.MainActivity.onCreate(MainActivity.java:22)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.app.Activity.performCreate(Activity.java:5122)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336)
02-25 05:38:40.462: E/AndroidRuntime(4342): ... 11 more
02-25 05:38:40.462: E/AndroidRuntime(4342): Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
02-25 05:38:40.462: E/AndroidRuntime(4342): <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
02-25 05:38:40.462: E/AndroidRuntime(4342): <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
02-25 05:38:40.462: E/AndroidRuntime(4342): at maps.e.ch.a(Unknown Source)
02-25 05:38:40.462: E/AndroidRuntime(4342): at maps.e.ay.a(Unknown Source)
02-25 05:38:40.462: E/AndroidRuntime(4342): at maps.e.ay.a(Unknown Source)
02-25 05:38:40.462: E/AndroidRuntime(4342): at maps.e.al.a(Unknown Source)
02-25 05:38:40.462: E/AndroidRuntime(4342): at maps.e.bg.a(Unknown Source)
02-25 05:38:40.462: E/AndroidRuntime(4342): at maps.e.bf.a(Unknown Source)
02-25 05:38:40.462: E/AndroidRuntime(4342): at eio.onTransact(SourceFile:107)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.os.Binder.transact(Binder.java:310)
02-25 05:38:40.462: E/AndroidRuntime(4342): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
02-25 05:38:40.462: E/AndroidRuntime(4342): at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
02-25 05:38:40.462: E/AndroidRuntime(4342): at com.google.android.gms.dynamic.a$4.b(Unknown Source)
02-25 05:38:40.462: E/AndroidRuntime(4342): at com.google.android.gms.dynamic.a.a(Unknown Source)
02-25 05:38:40.462: E/AndroidRuntime(4342): at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
02-25 05:38:40.462: E/AndroidRuntime(4342): at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
02-25 05:38:40.462: E/AndroidRuntime(4342): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
02-25 05:38:40.462: E/AndroidRuntime(4342): ... 21 more
答案 0 :(得分:2)
每SupportMapFragment.getMap(),它返回
null
如果片段的视图尚未准备好。如果片段生命周期尚未通过onCreateView(LayoutInflater,ViewGroup,Bundle),则会发生这种情况。如果Google Play服务不可用,也会发生这种情况。如果Google Play服务随后可用且片段已经通过onCreateView(LayoutInflater,ViewGroup,Bundle),则再次调用此方法将初始化并返回GoogleMap。
考虑configuring your initial state via XML attributes并将getMap().setMyLocationEnabled(true)
移至onLocationChanged
以确保您的地图已经布局和配置,而不是在onCreate