BadParcelableException在谷歌地图code [英] BadParcelableException in google maps code

查看:191
本文介绍了BadParcelableException在谷歌地图code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谷歌地图的运行稍作修改示例引发 BadParcelableException 在谷歌地图code。该经纬度类是parcelable但不能被发现。看来,谷歌地图code试图unparcel不是由它瓜分的对象。什么情况下,什么问题?

 包com.example.mapdemo;

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

进口android.os.Bundle;

公共类RawMapViewDemoActivity扩展android.support.v4.app.FragmentActivity {
    私人MapView类mMapView;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.raw_mapview_demo);

        mMapView =(图形页面)findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
    }

    @覆盖
    公共无效的onSaveInstanceState(包outState){
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);

        outState.putParcelable(标记,新的经纬度(0,0));
    }

    @覆盖
    保护无效onRestoreInstanceState(包savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);

        经纬度LL = savedInstanceState.getParcelable(标记);
    }
}
 

...

 致命异常:主要
java.lang.RuntimeException的:无法启动的活动
    ComponentInfo {com.example.mapdemo / com.example.mapdemo.RawMapViewDemoActivity}:
    android.os.BadParcelableException:ClassNotFoundException的时候解组:
    com.google.android.gms.maps.model.LatLng
   在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
   在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832)
   在android.app.ActivityThread.access $ 1600(ActivityThread.java:117)
   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935)
   在android.os.Handler.dispatchMessage(Handler.java:99)
   在android.os.Looper.loop(Looper.java:130)
   在android.app.ActivityThread.main(ActivityThread.java:3683)
   在java.lang.reflect.Method.invokeNative(本机方法)
   在java.lang.reflect.Method.invoke(Method.java:507)
   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   在dalvik.system.NativeStart.main(本机方法)

致:android.os.BadParcelableException:
ClassNotFoundException的解组时:com.google.android.gms.maps.model.LatLng
   在android.os.Parcel.readParcelable(Parcel.java:1958)
   在android.os.Parcel.readValue(Parcel.java:1846)
   在android.os.Parcel.readMapInternal(Parcel.java:2083)
   在android.os.Bundle.unparcel(Bundle.java:208)
   在android.os.Bundle.getBundle(Bundle.java:1078)
   在com.google.android.gms.maps.internal.MapStateHelper
       .getParcelableFromMapStateBundle(MapStateHelper.java:41)
   在maps.y.ae.a(来源不明)
   在maps.y.bm.onCreate(来源不明)
   在com.google.android.gms.maps.internal.IMapViewDelegate $存根
       .onTransact(IMapViewDelegate.java:66)
   在android.os.Binder.transact(Binder.java:279)
   在com.google.android.gms.maps.internal.IMapViewDelegate $ A $一个
       .onCreate(来源不明)
   在com.google.android.gms.maps.MapView $ b.onCreate(来源不明)
   在com.google.android.gms.internal.c $ 3.A(来源不明)
   在com.google.android.gms.internal.ib(来源不明)
   在com.google.android.gms.maps.MapView $ AA(来源不明)
   在com.google.android.gms.maps.MapView $ AA(来源不明)
   在com.google.android.gms.internal.ca(来源不明)
   在com.google.android.gms.internal.c.onCreate(来源不明)
   在com.google.android.gms.maps.MapView.onCreate(来源不明)
   在com.example.mapdemo.RawMapViewDemoActivity
       .onCreate(RawMapViewDemoActivity.java:40)
   在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
   在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
   ... 12更多
 

解决方案

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

在节电状态时,要小心将你的数据到包(你做得很好),然后转发MapView.onSaveInstanceState电话:

  @覆盖
公共无效的onSaveInstanceState(包outState){

    //调用转发之前,加入我们的经纬度阵列,否则会崩溃:
    _mapView.onSaveInstanceState(outState);

    //把你Parcelable在包:
    outState.putParcelableArray(myLatLng,新的经纬度(0,0));
}
 

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

  @覆盖
公共无效的onCreate(包savedInstanceState){

    //如果包不为空,让您的Parcelable:
    经纬度myLatLng = NULL;
    如果(savedInstanceState!= NULL){

        myLatLng =(经纬度)savedInstanceState.getParcelable(myLatLng);

        //从包中删除您Parcelable,否则会崩溃:
        savedInstanceState.remove(myLatLng);
    }

    //转移呼叫:
    _mapView.onCreate(savedInstanceState);
    setUpMapIfNeeded();
}
 

Running slightly modified example of Google Maps throws BadParcelableException in the Google Maps code. The LatLng class is parcelable but it cannot be found. It seems that Google Maps code is trying to unparcel the object that was not parcelled by it. What cases the problem?

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

解决方案

I tried the two previous answers without success, but I found another workaround :

When saving the state, be careful to forward the MapView.onSaveInstanceState call before adding your data to the Bundle (you did it well) :

@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) );
}

When restoring the state in the onCreate / onRestore, check if the bundle is not null, if so, get your parcel, and then remove it from the bundle before forwarding the call :

@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();
}

这篇关于BadParcelableException在谷歌地图code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆