gpt4 book ai didi

java - 更新到 api 19 后 Google Maps V2 崩溃

转载 作者:行者123 更新时间:2023-11-29 21:13:45 24 4
gpt4 key购买 nike

在我更新 sdk 管理器中的一些项目之前一切正常。现在,每当我尝试运行具有谷歌地图的应用程序时,我的应用程序就会崩溃。

list :

<?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>

主要 Activity :

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>

enter image description here

好的,我已经添加了

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

在 list 中。 list 已更新。

但是还是有错误:

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

最佳答案

根据 SupportMapFragment.getMap() , 它返回

null if the view of the fragment is not yet ready. This can happen if the fragment lifecyle have not gone through onCreateView(LayoutInflater, ViewGroup, Bundle) yet. This can also happen if Google Play services is not available. If Google Play services becomes available afterwards and the fragment have gone through onCreateView(LayoutInflater, ViewGroup, Bundle), calling this method again will initialize and return the GoogleMap.

考虑 configuring your initial state via XML attributes并将你的 getMap().setMyLocationEnabled(true) 移动到你的 onLocationChanged 以确保你的 map 已经布局和配置,而不是在 onCreate

关于java - 更新到 api 19 后 Google Maps V2 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21999061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com