Android:当我的应用程序通过单击主页键一段时间转到后台时,然后当我重新启动时,应用程序崩溃了

时间:2015-07-17 06:26:47

标签: android android-fragments

我发现异常是因为在onResume()中调用了super.onResume(),异常细节是:

07-08 10:10:00.483  26883-26883/E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.xxx.xxx, PID: 26883
    java.lang.RuntimeException: Unable to resume activity {com.xxx.xxx/com.xxx.xxx.MainActivity}: java.lang.NullPointerException: Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim' on a null object reference
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2986)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim' on a null object reference
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:714)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentActivity.onResume(FragmentActivity.java:426)
            at com.xxx.xxx.MainActivity.onResume(MainActivity.java:74)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
            at android.app.Activity.performResume(Activity.java:6076)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2975)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我认为原因是内存被回收,但我该如何解决呢?

我认为这是“MainActivity”的主要代码,它管理底部的四个片段

    public RadioButton rbTabLottery, rbTabAward, rbTabCircle, rbTabMine;
    private int selectedId = R.id.rb_tab_lottery;
    private Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        if (savedInstanceState == null) {
            Fragment fragment = getFragment(selectedId);
            getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,fragment).commit();
            currentFragment = fragment;
        }
    }

    @Override
    protected void initView() {//parent class is called
        rbTabLottery = findView(R.id.rb_tab_lottery);
        rbTabAward = findView(R.id.rb_tab_award);
        rbTabCircle = findView(R.id.rb_tab_circle);
        rbTabMine = findView(R.id.rb_tab_mine);

        rbTabLottery.setOnCheckedChangeListener(this);
        rbTabAward.setOnCheckedChangeListener(this);
        rbTabCircle.setOnCheckedChangeListener(this);
        rbTabMine.setOnCheckedChangeListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();     //the exception is here
        if (selectedId == R.id.rb_tab_award){
            rbTabAward.setChecked(true);
        }else if (selectedId == R.id.rb_tab_circle){
            rbTabCircle.setChecked(true);
        }else if (selectedId == R.id.rb_tab_mine){
            rbTabMine.setChecked(true);
        }else {
            selectedId = R.id.rb_tab_lottery;
            rbTabLottery.setChecked(true);
        }
        Fragment fragment = getFragment(selectedId);
        if (fragment != null){
            addOrShowFragment(getSupportFragmentManager().beginTransaction(), fragment);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked){
            selectedId = buttonView.getId();

            Fragment fragment = getFragment(selectedId);
            if (fragment != null){
                addOrShowFragment(getSupportFragmentManager().beginTransaction(),fragment);

            }
        }
    }

    private Fragment getFragment(int selectedId){
        switch (selectedId) {
            case R.id.rb_tab_lottery:
                return TabLotteryFragment.getInstance();
            case R.id.rb_tab_award:
                return TabAwardFragment.getInstance();
            case R.id.rb_tab_circle:
                return TabCircleFragment.getInstance();
            case R.id.rb_tab_mine:
                return TabMineFragment.getInstance();
            default:
                return TabLotteryFragment.getInstance();
        }
    }

    /**
     * @param transaction
     * @param fragment
     */
    private void addOrShowFragment(FragmentTransaction transaction,
                                   Fragment fragment) {
        if (currentFragment == fragment)
            return;

        if (!fragment.isAdded()) { 
            transaction.hide(currentFragment).add(R.id.fragment_container, fragment,
                    String.valueOf(selectedId)).commitAllowingStateLoss();
        } else {
            transaction.hide(currentFragment).show(fragment).commitAllowingStateLoss();
        }

        currentFragment = fragment;
    }

1 个答案:

答案 0 :(得分:0)

确保您的应用在恢复时处于有效状态。

有一些重要的方法,例如:

protected void onSaveInstanceState(Bundle outState){}

public void onRestoreInstanceState(Bundle savedInstanceState){}

protected void onResume() {}

请参阅此文档Activity LifecycleonResume()