从手机运行时Android应用程序崩溃,但在模拟器

时间:2017-07-10 07:28:54

标签: android android-fragments android-recyclerview

最近我的Android应用程序在真实设备上测试时崩溃了。但它在模拟器上测试时工作正常。我的应用程序具有实现RecyclerView的片段,当RecyclerView中的项目单击它时,它将启动新的Detail活动,并在单击后退按钮时崩溃。我的角色回到之前在设备上工作的提交,但它仍然崩溃。 这是错误日志:

07-10 14:07:15.146 12250-12250/com.luckyendey.polymarkapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.luckyendey.polymarkapp, PID: 12250
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.luckyendey.polymarkapp/com.luckyendey.polymarkapp.main.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.luckyendey.polymarkapp.bonsementara.BonSementaraHeaderTabOpenFragment$LayoutManagerType.ordinal()' on a null object reference
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:156)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:211)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5389)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.luckyendey.polymarkapp.bonsementara.BonSementaraHeaderTabOpenFragment$LayoutManagerType.ordinal()' on a null object reference
                                                                            at com.luckyendey.polymarkapp.bonsementara.BonSementaraHeaderTabOpenFragment.setRecyclerViewLayoutManager(BonSementaraHeaderTabOpenFragment.java:98)
                                                                            at com.luckyendey.polymarkapp.bonsementara.BonSementaraHeaderTabOpenFragment.onCreateView(BonSementaraHeaderTabOpenFragment.java:60)
                                                                            at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                                            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                                            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.dispatchActivityCreated(FragmentManager.java:2900)
                                                                            at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2207)
                                                                            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1326)
                                                                            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.dispatchActivityCreated(FragmentManager.java:2900)
                                                                            at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
                                                                            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:603)
                                                                            at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                                            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
                                                                            at android.app.Activity.performStart(Activity.java:6006)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:156) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:211) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5389) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

这是我的代码:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.tab_purchase_request_open, container, false);

    access_token = GlobalMethod.getPref("access_token",getContext());

    presenter = new PurchaseRequestHeaderOpenPresenter(this);
    presenter.getListDataPurchaseRequest(access_token,"");
    presenter.getRequestType(access_token);

    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_purchase_request_open);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;

    if (savedInstanceState != null) {
        // Restore saved layout manager type.
        mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState
                .getSerializable(KEY_LAYOUT_MANAGER); // this is return null that causing the error
    }

    setRecyclerViewLayoutManager(mCurrentLayoutManagerType);

    swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_purchase_request_open);
    swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary,R.color.colorAccent,R.color.colorPrimary);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            presenter.getListDataPurchaseRequest(access_token,"");
        }
    });

    return  view;
}

在模拟器上onCreateView在后退时单击详细活动时不会触发,但在设备中再次调用。

1 个答案:

答案 0 :(得分:0)

通过关闭开发人员选项下的不要保持活动,我已经解决了这个问题。我忘了我已启用它,现在我的应用程序运行良好。