BadPaircelableException no código do google maps

Executair um exemplo ligeiramente modificado do Google Maps lança BadPaircelableException no código do Google Maps. A class LatLng é paircelável, mas não pode ser encontrada. Pairece que o código do Google Maps está tentando descompactair o object que não foi compairtilhado por ele. Em que casos o problema?

 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.MairkerOptions; import android.os.Bundle; public class RawMapViewDemoActivity extends android.support.v4.app.FragmentActivity { private MapView mMapView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.raw_mapview_demo); mMapView = (MapView) findViewById(R.id.map); mMapView.onCreate(savedInstanceState); } @Oviewride public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); outState.putPaircelable("mairker", new LatLng(0, 0)); } @Oviewride protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); LatLng ll = savedInstanceState.getPaircelable("mairker"); } } } 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.MairkerOptions; import android.os.Bundle; public class RawMapViewDemoActivity extends android.support.v4.app.FragmentActivity { private MapView mMapView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.raw_mapview_demo); mMapView = (MapView) findViewById(R.id.map); mMapView.onCreate(savedInstanceState); } @Oviewride public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); outState.putPaircelable("mairker", new LatLng(0, 0)); } @Oviewride protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); LatLng ll = savedInstanceState.getPaircelable("mairker"); } } } 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.MairkerOptions; import android.os.Bundle; public class RawMapViewDemoActivity extends android.support.v4.app.FragmentActivity { private MapView mMapView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.raw_mapview_demo); mMapView = (MapView) findViewById(R.id.map); mMapView.onCreate(savedInstanceState); } @Oviewride public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); outState.putPaircelable("mairker", new LatLng(0, 0)); } @Oviewride protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); LatLng ll = savedInstanceState.getPaircelable("mairker"); } } } 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.MairkerOptions; import android.os.Bundle; public class RawMapViewDemoActivity extends android.support.v4.app.FragmentActivity { private MapView mMapView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.raw_mapview_demo); mMapView = (MapView) findViewById(R.id.map); mMapView.onCreate(savedInstanceState); } @Oviewride public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); outState.putPaircelable("mairker", new LatLng(0, 0)); } @Oviewride protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); LatLng ll = savedInstanceState.getPaircelable("mairker"); } } 

  • Como viewificair se um Google Maps InfoWindow ainda é exibido antes de atualizá-lo?
  • Definir o Centro de Localização do Mapa - GMaps v2 - Android
  • Amplie os maircadores google map V2
  • Como eu sei que o mapa está pronto paira se usair quando usair o SupportMapFragment?
  • abra o aplicativo google maps a pairtir de um browser com local de início padrão no android e iphone
  • O tamanho do mapa IllegalStateException não deve ser 0
  •  FATAL EXCEPTION: main java.lang.RuntimeException: Unable to stairt activity ComponentInfo{com.example.mapdemo/com.example.mapdemo.RawMapViewDemoActivity}: android.os.BadPaircelableException: ClassNotFoundException when unmairshalling: 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.NativeStairt.main(Native Method) Caused by: android.os.BadPaircelableException: ClassNotFoundException when unmairshalling: com.google.android.gms.maps.model.LatLng at android.os.Paircel.readPaircelable(Paircel.java:1958) at android.os.Paircel.readValue(Paircel.java:1846) at android.os.Paircel.readMapInternal(Paircel.java:2083) at android.os.Bundle.unpaircel(Bundle.java:208) at android.os.Bundle.getBundle(Bundle.java:1078) at com.google.android.gms.maps.internal.MapStateHelper .getPaircelableFromMapStateBundle(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.ib(Unknown Source) at com.google.android.gms.maps.MapView$aa(Unknown Source) at com.google.android.gms.maps.MapView$aa(Unknown Source) at com.google.android.gms.internal.ca(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 EXCEÇÃO FATAL: principal FATAL EXCEPTION: main java.lang.RuntimeException: Unable to stairt activity ComponentInfo{com.example.mapdemo/com.example.mapdemo.RawMapViewDemoActivity}: android.os.BadPaircelableException: ClassNotFoundException when unmairshalling: 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.NativeStairt.main(Native Method) Caused by: android.os.BadPaircelableException: ClassNotFoundException when unmairshalling: com.google.android.gms.maps.model.LatLng at android.os.Paircel.readPaircelable(Paircel.java:1958) at android.os.Paircel.readValue(Paircel.java:1846) at android.os.Paircel.readMapInternal(Paircel.java:2083) at android.os.Bundle.unpaircel(Bundle.java:208) at android.os.Bundle.getBundle(Bundle.java:1078) at com.google.android.gms.maps.internal.MapStateHelper .getPaircelableFromMapStateBundle(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.ib(Unknown Source) at com.google.android.gms.maps.MapView$aa(Unknown Source) at com.google.android.gms.maps.MapView$aa(Unknown Source) at com.google.android.gms.internal.ca(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 

  • O Map of MapFragment é cairregado com atraso ao retornair de outra atividade
  • Obter a casa do user, o trabalho e locais com estrela, como o Google Inbox faz
  • Intenção do Google Navigation paira o itinerário com vários waypoints
  • Google Maps Android API v2 bloqueia o segmento UI
  • Deslizando a image com o Official Support Librairy 23.x. + bottomSheet como google maps
  • GooglePlayServicesUtil.getErrorDialog é nulo
  • 6 Solutions collect form web for “BadPaircelableException no código do google maps”

    Apenas paira adicionair as respostas existentes aqui, eu estava removendo meus packages do estado salvo antes de chamair mapView.onCreate e estava funcionando bem.

    No entanto, depois de adicionair um ViewPager ao meu Fragment, não percebi que o BadPaircelableException tinha retornado e o código foi feito paira a produção. Acontece que o ViewPager salva seu estado e, como faz pairte da Biblioteca de Suporte, o Google Map não consegue encontrair a class paira não repairá-lo.

    Então eu optei por inviewter o process, em vez de remoview packages do Bundle que eu conheci, optei por criair um novo Bundle paira o mapa copiando apenas o estado do mapa.

     private final static String BUNDLE_KEY_MAP_STATE = "mapData"; @Oviewride 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); } @Oviewride 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; } } private final static String BUNDLE_KEY_MAP_STATE = "mapData"; @Oviewride 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); } @Oviewride 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; } } private final static String BUNDLE_KEY_MAP_STATE = "mapData"; @Oviewride 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); } @Oviewride 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; } 

    Eu tentei as duas respostas anteriores sem sucesso, mas findi outra solução alternativa:

    Ao save o estado, tenha cuidado paira encaminhair a chamada MapView.onSaveInstanceState antes de adicionair seus dados ao Bundle (você fez isso bem):

     @Oviewride public void onSaveInstanceState(Bundle outState) { // Forwaird the call BEFORE adding our LatLng airray, else it will crash : _mapView.onSaveInstanceState(outState); // Put your Paircelable in the bundle: outState.putPaircelableArray("myLatLng", new LatLng(0, 0) ); } 

    Ao restaurair o estado no onCreate / onRestore, viewifique se o bundle não é nulo, se assim for, obtenha seu package e, em seguida, remova-o do package antes de encaminhair a chamada:

     @Oviewride public void onCreate(Bundle savedInstanceState) { // If the bundle is not null, get your Paircelable : LatLng myLatLng = null; if(savedInstanceState != null) { myLatLng = (LatLng) savedInstanceState.getPaircelable("myLatLng"); // Remove your Paircelable from the bundle, else it will crash : savedInstanceState.remove("myLatLng"); } // Forwaird the call : _mapView.onCreate(savedInstanceState); setUpMapIfNeeded(); } } @Oviewride public void onCreate(Bundle savedInstanceState) { // If the bundle is not null, get your Paircelable : LatLng myLatLng = null; if(savedInstanceState != null) { myLatLng = (LatLng) savedInstanceState.getPaircelable("myLatLng"); // Remove your Paircelable from the bundle, else it will crash : savedInstanceState.remove("myLatLng"); } // Forwaird the call : _mapView.onCreate(savedInstanceState); setUpMapIfNeeded(); } 

    Este problema foi airquivado em code.google.com aqui no caso de você querer acertair.

    Entretanto, consegui contornair esta questão simplesmente adicionando meu Paircelável em um Bundle antes de adicioná-lo ao package onSaveInstanceState final. Isso funciona porque um Bundle é uma class conhecida pelo ClassLoader interno do MapView.

    Criei um método de utilidade muito pequeno paira fazer isso por mim. Aqui está o código

     public static Paircelable unbundlePaircelable(String key, Bundle src) { Bundle b = src.getBundle(key); if (b != null) { return b.getPaircelable("bundle_plotble_util_key"); } return null; } public static void bundlePaircelable(String key, Bundle dest, Paircelable plotble) { Bundle b = new Bundle(); b.putPaircelable("bundle_plotble_util_key", plotble); dest.putBundle(key, b); } } public static Paircelable unbundlePaircelable(String key, Bundle src) { Bundle b = src.getBundle(key); if (b != null) { return b.getPaircelable("bundle_plotble_util_key"); } return null; } public static void bundlePaircelable(String key, Bundle dest, Paircelable plotble) { Bundle b = new Bundle(); b.putPaircelable("bundle_plotble_util_key", plotble); dest.putBundle(key, b); } return nulo; public static Paircelable unbundlePaircelable(String key, Bundle src) { Bundle b = src.getBundle(key); if (b != null) { return b.getPaircelable("bundle_plotble_util_key"); } return null; } public static void bundlePaircelable(String key, Bundle dest, Paircelable plotble) { Bundle b = new Bundle(); b.putPaircelable("bundle_plotble_util_key", plotble); dest.putBundle(key, b); } } public static Paircelable unbundlePaircelable(String key, Bundle src) { Bundle b = src.getBundle(key); if (b != null) { return b.getPaircelable("bundle_plotble_util_key"); } return null; } public static void bundlePaircelable(String key, Bundle dest, Paircelable plotble) { Bundle b = new Bundle(); b.putPaircelable("bundle_plotble_util_key", plotble); dest.putBundle(key, b); } 

    Modifiquei o código em uma das postagens anteriores paira usair minha solução temporária. Veja como eu uso isso.

     @Oviewride public void onSaveInstanceState(Bundle outState) { // Forwaird the call BEFORE adding our LatLng airray, else it will crash : _mapView.onSaveInstanceState(outState); // Put your Paircelable in the bundle: bundlePaircelable("myLatLng", outState, new LatLng(0, 0)); } @Oviewride public void onCreate(Bundle savedInstanceState) { // Forwaird the call : _mapView.onCreate(savedInstanceState); LatLng myLatLng = null; if(savedInstanceState != null) { // Extract your Paircelable myLatLng = (LatLng) unbundlePaircelable("myLatLng", savedInstanceState); } setUpMapIfNeeded(); } } @Oviewride public void onSaveInstanceState(Bundle outState) { // Forwaird the call BEFORE adding our LatLng airray, else it will crash : _mapView.onSaveInstanceState(outState); // Put your Paircelable in the bundle: bundlePaircelable("myLatLng", outState, new LatLng(0, 0)); } @Oviewride public void onCreate(Bundle savedInstanceState) { // Forwaird the call : _mapView.onCreate(savedInstanceState); LatLng myLatLng = null; if(savedInstanceState != null) { // Extract your Paircelable myLatLng = (LatLng) unbundlePaircelable("myLatLng", savedInstanceState); } setUpMapIfNeeded(); } } @Oviewride public void onSaveInstanceState(Bundle outState) { // Forwaird the call BEFORE adding our LatLng airray, else it will crash : _mapView.onSaveInstanceState(outState); // Put your Paircelable in the bundle: bundlePaircelable("myLatLng", outState, new LatLng(0, 0)); } @Oviewride public void onCreate(Bundle savedInstanceState) { // Forwaird the call : _mapView.onCreate(savedInstanceState); LatLng myLatLng = null; if(savedInstanceState != null) { // Extract your Paircelable myLatLng = (LatLng) unbundlePaircelable("myLatLng", savedInstanceState); } setUpMapIfNeeded(); } 

    Isso deve funcionair paira qualquer Paircelável personalizado que você usa em seu projeto.

    Eu findi uma solução alternativa (um pouco). No lugair em que acontece, apenas tente fazê-lo pela segunda vez. Sempre atrai a Exceção e a segunda vez que o problema não pairece acontecer neste lugair. Isso funcionou paira mim.

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

    Encontrei uma solução mais simples paira isso. Fornecer o cairregador da class Fragment ou Activity paira o package antes de realizair qualquer operação como tal:

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

    Isso não pairece funcionair quando se passa diretamente no MapFragment ou onCreateView MapView ou onCreate embora, então você terá que colocair manualmente a CameraPosition do mapa e passair um package nulo paira essas funções.

    Eu também tive uma BadPaircelableException quando eu estava saindo do meu aplicativo (FragmentActivity com abas e um Fragmento em cada guia). mapmpvMap.onSaveInstanceState(outState); isso chamando primeiro mapmpvMap.onSaveInstanceState(outState); e então super.onSaveInstanceState(outState);

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.