我已经在我的 android 应用程序的生产 apk 中遇到异常 1 天了:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.a.b/com.a.b.c.d.HomeActivity}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: android.support.v7.widget.RecyclerView$SavedState
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1654)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1670)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3695)
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:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: android.support.v7.widget.RecyclerView$SavedState
at android.os.Parcel.readParcelable(Parcel.java:1958)
at android.os.Parcel.readValue(Parcel.java:1846)
at android.os.Parcel.readSparseArrayInternal(Parcel.java:2112)
at android.os.Parcel.readSparseArray(Parcel.java:1568)
at android.os.Parcel.readValue(Parcel.java:1903)
at android.os.Parcel.readMapInternal(Parcel.java:2083)
at android.os.Bundle.unparcel(Bundle.java:208)
at android.os.Bundle.getSparseParcelableArray(Bundle.java:1167)
at android.support.design.internal.NavigationMenuPresenter.onRestoreInstanceState(Unknown Source)
at android.support.v7.view.menu.i.dispatchRestoreInstanceState(Unknown Source)
at android.support.v7.view.menu.i.restorePresenterStates(Unknown Source)
at android.support.design.widget.NavigationView.onRestoreInstanceState(Unknown Source)
at android.view.View.dispatchRestoreInstanceState(View.java:6287)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1211)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215)
at android.view.View.restoreHierarchyState(View.java:6266)
at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1506)
at android.app.Activity.onRestoreInstanceState(Activity.java:847)
at android.app.Activity.performRestoreInstanceState(Activity.java:819)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1632)
... 11 more
以上所有崩溃报告均来自 Android 2.3.3 - 2.3.7 版本。不确定是什么原因造成的。
我也搜索了其他类似的帖子,但答案是围绕一个实现 parcelable 的自定义对象,我没有在我的应用程序中实现。
我在我的应用程序中使用最新版本的 android 支持库和播放服务,如下所示:
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.google.android.gms:play-services-analytics:8.4.0'
compile 'com.google.android.gms:play-services-ads:8.4.0'
compile 'com.google.android.gms:play-services-appinvite:8.4.0'
compile 'com.google.android.gms:play-services-plus:8.4.0'
这是失败的活动的代码(我觉得相关,如果不是,请告诉我还需要什么):
private void initNavigationMenu() {
mNavigationView = (NavigationView) findViewById(R.id.nav_view);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
return onNavItemSelected(menuItem);
}
});
Menu menu = mNavigationView.getMenu();
MenuItem menuItem = menu.findItem(R.id.action_tutorial);
IconDrawable iconDrawable = new IconDrawable(this, MaterialIcons.md_ondemand_video)
.colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem = menu.findItem(R.id.action_settings);
iconDrawable = new IconDrawable(this, MaterialIcons.md_settings).colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem = menu.findItem(R.id.action_images_all);
iconDrawable = new IconDrawable(this, EntypoIcons.entypo_images).colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem = menu.findItem(R.id.action_images_screenshots);
iconDrawable = new IconDrawable(this, MaterialIcons.md_camera).colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem = menu.findItem(R.id.action_favorites);
iconDrawable = new IconDrawable(this, MaterialIcons.md_favorite).colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem = menu.findItem(R.id.action_translate);
iconDrawable = new IconDrawable(this, MaterialIcons.md_translate).colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem = menu.findItem(R.id.action_feedback);
iconDrawable = new IconDrawable(this, MaterialIcons.md_feedback).colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem = menu.findItem(R.id.action_rate);
iconDrawable = new IconDrawable(this, MaterialIcons.md_rate_review).colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem = menu.findItem(R.id.action_invite);
iconDrawable = new IconDrawable(this, MaterialIcons.md_insert_invitation).colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem = menu.findItem(R.id.action_facebook);
iconDrawable = new IconDrawable(this, FontAwesomeIcons.fa_facebook).colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem = menu.findItem(R.id.action_twitter);
iconDrawable = new IconDrawable(this, FontAwesomeIcons.fa_twitter).colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem = menu.findItem(R.id.action_google_plus);
iconDrawable = new IconDrawable(this, FontAwesomeIcons.fa_google_plus).colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem = menu.findItem(R.id.action_remove_ads);
iconDrawable = new IconDrawable(this, MaterialIcons.md_attach_money).colorRes(R.color.black);
menuItem.setIcon(iconDrawable);
menuItem.setVisible(!BuildConfig.IS_PRO);
setHeaderView();
}
private void setHeaderView() {
boolean initializePlusOneButton = false;
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int code = googleApiAvailability.isGooglePlayServicesAvailable(this);
if (code == ConnectionResult.SUCCESS) {
initializePlusOneButton = true;
} else if (googleApiAvailability.isUserResolvableError(code)) {
googleApiAvailability.showErrorDialogFragment(this, code, REQ_CODE_GOOGLE_API_ERROR);
}
View headerView = LayoutInflater.from(this).inflate(
initializePlusOneButton ?
R.layout.activity_home_drawer_header :
R.layout.activity_home_drawer_header_wo_plus_button, null);
if (initializePlusOneButton) {
mPlusOneButton = (PlusOneButton) headerView.findViewById(R.id.plus_one_button);
}
mNavigationView.addHeaderView(headerView);
gLogger.debug(String.format(
"setHeaderView(): initialize plusOneButton=%s", initializePlusOneButton
));
}
R.layout.activity_home_drawer_layout.xml 的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="?attr/colorPrimary"
android:orientation="vertical">
<com.andexert.library.RippleView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:padding="10dp"
app:rv_rippleDuration="300"
app:rv_type="rectangle">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.joanzapata.iconify.widget.IconTextView
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_above="@+id/app_name"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="{md-camera}"
android:textColor="@color/white"
android:textSize="50sp" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="italic|bold" />
<com.google.android.gms.plus.PlusOneButton
xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
android:id="@+id/plus_one_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
plus:annotation="bubble"
plus:size="medium" />
</LinearLayout>
</LinearLayout>
</com.andexert.library.RippleView>
</LinearLayout>
R.layout.activity_home_drawer_header_wo_plus_button.xml 的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="?attr/colorPrimary"
android:orientation="vertical">
<com.andexert.library.RippleView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:padding="10dp"
app:rv_rippleDuration="300"
app:rv_type="rectangle">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.joanzapata.iconify.widget.IconTextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_above="@+id/app_name"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:layout_height="0dip"
android:text="{md-camera}"
android:textColor="@color/white"
android:textSize="80sp" />
<TextView
android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="italic|bold" />
</LinearLayout>
</com.andexert.library.RippleView>
</LinearLayout>