片段破坏时,Recycler View崩溃

时间:2017-06-16 08:23:16

标签: android android-fragments android-recyclerview

每次关闭应用或更改标签时,我都会崩溃:

FATAL EXCEPTION: main
   Process: com.pinmedia.mediaguide, PID: 30982
   java.lang.NoClassDefFoundError: rt
       at rs.<clinit>(SourceFile:17)
       at android.support.v7.widget.RecyclerView.onSaveInstanceState(SourceFile:201)
       at android.view.View.dispatchSaveInstanceState(View.java:12922)
       at android.view.ViewGroup.dispatchFreezeSelfOnly(ViewGroup.java:2763)
       at android.support.v7.widget.RecyclerView.dispatchSaveInstanceState(SourceFile:220)
       at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2749)
       at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2749)
       at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2749)
       at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2749)
       at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2749)
       at android.view.View.saveHierarchyState(View.java:12905)
       at android.support.v4.app.FragmentManagerImpl.saveFragmentViewState(FragmentManager.java:2594)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1378)
       at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
       at android.support.v4.app.FragmentManagerImpl.dispatchDestroyView(FragmentManager.java:2943)
       at android.support.v4.app.Fragment.performDestroyView(Fragment.java:2414)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1381)
       at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
       at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:753)
       at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
       at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
       at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
       at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5584)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
       at dalvik.system.NativeStart.main(Native Method)

我在该片段中使用Horizo​​ntal RecyclerView:

RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.itv_submenu_recycler_view);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new SubMenuItemsAdapter(subMenuTabChangeListener);
mRecyclerView.setAdapter(mAdapter);

和回收者视图是

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    android:id="@+id/itv_submenu_recycler_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" />

我正在使用以下库来进行recyclerView:

compile 'com.android.support:support-v13:25.0.0'
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.android.support:recyclerview-v7:25.3.+'

此问题仅出现在一部手机(Kitkat设备)上。 请帮我解决这个崩溃。感谢

1 个答案:

答案 0 :(得分:1)

您使用支持库25.3.0,可能是发生崩溃的手机没有在手机上更新最新的Google Play服务。

检查

private void checkGooglePlayServices(){
    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int code = api.isGooglePlayServicesAvailable(this);
    if (code == ConnectionResult.SUCCESS) {
        onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null);
    } else if (api.isUserResolvableError(code) &&
            api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) {
        // wait for onActivityResult call (see below)
    } else {

        Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case REQUEST_GOOGLE_PLAY_SERVICES:
            if (resultCode == Activity.RESULT_OK) {
                loadData();
            }
            break;

        default:
            super.onActivityResult(requestCode, resultCode, data);
    }
}

@wrkwrk伤心可能是一种可能性,但通常更多的手机会出现问题,而不仅仅是一个。

在另一个节点上尝试不使用Gradle中的+指示器,每次尝试构建应用程序时,Gradle都必须检查最新版本是什么,并且只花费构建时间。