gpt4 book ai didi

android - 谷歌地图代码中的 BadParcelableException

转载 作者:IT老高 更新时间:2023-10-28 23:38:19 32 4
gpt4 key购买 nike

运行稍加修改的 Google map 示例会在 Google map 代码中引发 BadParcelableExceptionLatLng 类是 parcelable 但找不到。谷歌地图代码似乎试图解开未被它包裹的对象。什么情况下的问题?

package com.example.mapdemo;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;

public class RawMapViewDemoActivity extends android.support.v4.app.FragmentActivity {
private MapView mMapView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.raw_mapview_demo);

mMapView = (MapView) findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);

outState.putParcelable("marker", new LatLng(0, 0));
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);

LatLng ll = savedInstanceState.getParcelable("marker");
}
}

...

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.mapdemo/com.example.mapdemo.RawMapViewDemoActivity}:
android.os.BadParcelableException: ClassNotFoundException when unmarshalling:
com.google.android.gms.maps.model.LatLng
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832)
at android.app.ActivityThread.access$1600(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

Caused by: android.os.BadParcelableException:
ClassNotFoundException when unmarshalling: com.google.android.gms.maps.model.LatLng
at android.os.Parcel.readParcelable(Parcel.java:1958)
at android.os.Parcel.readValue(Parcel.java:1846)
at android.os.Parcel.readMapInternal(Parcel.java:2083)
at android.os.Bundle.unparcel(Bundle.java:208)
at android.os.Bundle.getBundle(Bundle.java:1078)
at com.google.android.gms.maps.internal.MapStateHelper
.getParcelableFromMapStateBundle(MapStateHelper.java:41)
at maps.y.ae.a(Unknown Source)
at maps.y.bm.onCreate(Unknown Source)
at com.google.android.gms.maps.internal.IMapViewDelegate$Stub
.onTransact(IMapViewDelegate.java:66)
at android.os.Binder.transact(Binder.java:279)
at com.google.android.gms.maps.internal.IMapViewDelegate$a$a
.onCreate(Unknown Source)
at com.google.android.gms.maps.MapView$b.onCreate(Unknown Source)
at com.google.android.gms.internal.c$3.a(Unknown Source)
at com.google.android.gms.internal.i.b(Unknown Source)
at com.google.android.gms.maps.MapView$a.a(Unknown Source)
at com.google.android.gms.maps.MapView$a.a(Unknown Source)
at com.google.android.gms.internal.c.a(Unknown Source)
at com.google.android.gms.internal.c.onCreate(Unknown Source)
at com.google.android.gms.maps.MapView.onCreate(Unknown Source)
at com.example.mapdemo.RawMapViewDemoActivity
.onCreate(RawMapViewDemoActivity.java:40)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
... 12 more

最佳答案

只是为了在此处添加现有答案,在调用 mapView.onCreate 之前,我一直在将我的包裹从保存状态中删除,它工作正常。

但是,在我的 Fragment 添加 ViewPager 之后,我没有意识到 BadParcelableException 已经返回并且代码已投入生产。事实证明 ViewPager 也保存了它的状态,因为它是支持库的一部分,谷歌地图找不到类来解包它。

所以我选择了反转这个过程,而不是从我知道的 Bundle 中删除 Parcel,我选择为 map 创建一个新的 Bundle,只复制 map 的状态。

private final static String BUNDLE_KEY_MAP_STATE = "mapData";

@Override
public void onSaveInstanceState(Bundle outState) {
// Save the map state to it's own bundle
Bundle mapState = new Bundle();
mapView.onSaveInstanceState(mapState);
// Put the map bundle in the main outState
outState.putBundle(BUNDLE_KEY_MAP_STATE, mapState);
super.onSaveInstanceState(outState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
mapView = (MapView) view.findViewById(R.id.mapView);
mapView.getMapAsync(this);

Bundle mapState = null;
if (savedInstanceState != null) {
// Load the map state bundle from the main savedInstanceState
mapState = savedInstanceState.getBundle(BUNDLE_KEY_MAP_STATE);
}

mapView.onCreate(mapState);
return view;
}

关于android - 谷歌地图代码中的 BadParcelableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13900322/

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