2012-12-16 50 views
14

运行谷歌地图的稍微修改例如,在谷歌地图代码抛出BadParcelableException。该LatLng类是parcelable但它不能被发现。看来,谷歌地图代码试图unparcel不是由它瓜分的对象。什么情况下的问题?BadParcelableException在谷歌地图代码

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 
+0

其中是在代码 –

+0

mMapView.onCreate(savedInstanceState)RawMapViewDemoActivity.java:40线;对不起,我编辑了简洁的代码。 – user1611728

+0

如果您评论'mMapView.onCreate(savedInstanceState);'线,那么它的工作与否? –

回答

0

我找到了一个解决方法(还挺)。在发生的地方只是尝试第二次做到这一点。它总是捕捉异常,并且第二次在这个地方看起来似乎不会发生问题。这对我有效。

try { 
    mapIntent.putExtra(Intents.EXTRA_LATLNG, new LatLng(
     store.getLatitude(), store.getLongitude())); 
} catch (BadParcelableException e) { 
    mapIntent.putExtra(Intents.EXTRA_LATLNG, new LatLng(
     store.getLatitude(), store.getLongitude())); 
} 
0

我发现了一个更简单的解决方法。提供您的片段或活动的类加载器捆绑perfoming任何操作如此前:

savedInstanceState.setClassLoader(getClass().getClassLoader()); 

这似乎并不直接传递到MapFragment或MapView类的onCreateView或的onCreate虽然在工作,所以你必须手动来将地图的CameraPosition缩小并将空包传递到这些函数中。

+0

您是否描述了您描述的解决方法示例? – speedynomads

12

我尝试没有成功前两个答案,但我发现了另一个解决方法:

在保存状态,小心将你的数据的包(你做得很好)之前转发MapView.onSaveInstanceState通话:

@Override 
public void onSaveInstanceState(Bundle outState) { 

    // Forward the call BEFORE adding our LatLng array, else it will crash : 
    _mapView.onSaveInstanceState(outState); 

    // Put your Parcelable in the bundle: 
    outState.putParcelableArray("myLatLng", new LatLng(0, 0)); 
} 

当在OnCreate/OnRestore中恢复状态,检查包不为空,如果是的话,把你的包,然后转移呼叫前,从包中取出:

@Override 
public void onCreate(Bundle savedInstanceState) { 

    // If the bundle is not null, get your Parcelable : 
    LatLng myLatLng = null; 
    if(savedInstanceState != null) { 

     myLatLng = (LatLng) savedInstanceState.getParcelable("myLatLng"); 

     // Remove your Parcelable from the bundle, else it will crash : 
     savedInstanceState.remove("myLatLng"); 
    } 

    // Forward the call : 
    _mapView.onCreate(savedInstanceState); 
    setUpMapIfNeeded(); 
} 
+0

此修补程序适用于我! thx – Sulfkain

+0

你救了我的命! :) – IlyaEremin

0

我也有一个BadParcelableException当我离开我的应用程序(FragmentActivity有标签和在每个标签的片段)。我通过调用第一mapmpvMap.onSaveInstanceState(outState);然后super.onSaveInstanceState(outState);

3

这个问题,因为你会想出演其案件被提交code.google.com上here解决了这个。

在平均时间,我管理,只需添加我的Parcelable到一个捆绑其添加到最终捆绑的onSaveInstanceState之前得到解决此问题。这是可行的,因为Bundle是MapView内部ClassLoader的已知类。

我已经创建了两个非常小的util的方法为我做到这一点。这里的代码

public static Parcelable unbundleParcelable(String key, Bundle src) { 
    Bundle b = src.getBundle(key); 
    if (b != null) { 
     return b.getParcelable("bundle_parcelable_util_key"); 
    } 
    return null; 
} 

public static void bundleParcelable(String key, Bundle dest, Parcelable parcelable) { 
    Bundle b = new Bundle(); 
    b.putParcelable("bundle_parcelable_util_key", parcelable); 
    dest.putBundle(key, b); 
} 

我修改了以前的帖子之一使用我的临时解决方案中的代码。以下是我如何使用它。

@Override 
public void onSaveInstanceState(Bundle outState) { 

    // Forward the call BEFORE adding our LatLng array, else it will crash : 
    _mapView.onSaveInstanceState(outState); 

    // Put your Parcelable in the bundle: 
    bundleParcelable("myLatLng", outState, new LatLng(0, 0)); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // Forward the call : 
    _mapView.onCreate(savedInstanceState); 

    LatLng myLatLng = null; 
    if(savedInstanceState != null) { 
     // Extract your Parcelable 
     myLatLng = (LatLng) unbundleParcelable("myLatLng", savedInstanceState); 
    } 

    setUpMapIfNeeded(); 
} 

这应该适用于您在项目中使用的任何自定义Parcelable。

+0

完美,你的答案奏效。 –

+0

我很高兴代码对某人有用。干杯! –

31

只是为了增加现有答案,在拨打mapView.onCreate之前,我一直从保存的状态中删除我的包裹,并且它工作正常。

然而,在我的片段中添加ViewPager后,我并没有意识到BadParcelableException已经返回并且代码已经生成。事实证明,ViewPager也保存了它的状态,因为它是支持库的一部分,所以Google Map无法找到该类来对它进行解码。

所以我选择反转过程,而不是从我知道的Bundle中删除Parcel,我选择为地图复制仅包含地图状态的新Bundle。

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; 
} 
+0

似乎有一个正在进行的线程:https://code.google.com/p/gmaps-api-issues/issues/detail?id=6237#c9 这是一个合理的答案,并为我工作的很好 –

+0

非常感谢你!工作完美! –