片段中的getActivity()返回null。父活动中FragmentPagerAdapter的问题?

时间:2015-11-26 17:31:35

标签: java android android-fragments

我有一个活动,主持一个包含两个片段的viewPager。我遇到了一个问题,即我的片段中对getActivity()的调用在活动被销毁然后重新创建后返回null(我正在更改系统设置,而我的应用程序在后台暂停,导致要销毁然后重新创建的活动)。这是我的父活动的样子。

public class MainActivity extends AppCompatActivity {

private CalculatorPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
private ViewPager mViewPager;
private final int HISTORY_PAGE_INDEX = 1;
private final int CALCULATOR_PAGE_INDEX = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new CalculatorPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        /**
         * Refreshes the listView in the history fragment whenever the user moves to it.
         * @param position
         */
        @Override
        public void onPageSelected(int position) {
            CalculatorPagerAdapter fragmentPagerAdapter = (CalculatorPagerAdapter) mViewPager.getAdapter();
            if(position == HISTORY_PAGE_INDEX){
                ((HistoryFragment) fragmentPagerAdapter.getItem(HISTORY_PAGE_INDEX)).refreshHistory();
            }else if(position == CALCULATOR_PAGE_INDEX){
                //((CalculatorFragment) fragmentPagerAdapter.getItem(CALCULATOR_PAGE_INDEX)).refreshCalculator();
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

}

@Override
public void onDestroy(){
    super.onDestroy();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
        return true;
    }

    return super.onOptionsItemSelected(item);
}


/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class CalculatorPagerAdapter extends FragmentPagerAdapter {

    HistoryFragment historyFragment;
    CalculatorFragment calculatorFragment;

    public CalculatorPagerAdapter(FragmentManager fm) {
        super(fm);
        historyFragment = new HistoryFragment();
        calculatorFragment = new CalculatorFragment();
    }


    @Override
    public Fragment getItem(int position) {
        if (position ==0) return calculatorFragment;
        else return historyFragment;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Calculator";
            case 1:
                return "History";
        }
        return null;
    }
}

}

我注意到活动和片段的生命周期调用按以下顺序发生。

初始创建(正常)

  1. activity.onCreate
  2. fragment.onAttach
  3. fragment.onCreateView
  4. 当我更改触发活动销毁和娱乐的系统设置时(此处发布)

    1. activity.onDestroy
    2. fragment.onDestroy
    3. fragment.onAttach(没有父活动?)
    4. activity.onCreate
    5. fragment.onCreateView
    6. 在此之后,片段中的getActivity返回null。我在父活动中错误地处理了我的片段吗?

      这是尝试使用null getActivity()导致崩溃的logcat。这是由附加到我的viewpager的OnPageChangeListener中的onPageSelected方法触发的。

      11-26 12:56:21.739 31769-31769/com.example.keegan.securetipping I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@d035425 time:1218086194
      11-26 12:56:24.864 31769-31769/com.example.keegan.securetipping E/InputEventReceiver: Exception dispatching input event.
      11-26 12:56:24.864 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' on a null object reference
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at com.example.keegan.securetipping.HistoryFragment.refreshHistory(HistoryFragment.java:88)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at com.example.keegan.securetipping.MainActivity$1.onPageSelected(MainActivity.java:54)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1794)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:569)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:553)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2102)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.View.dispatchTouchEvent(View.java:8547)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2400)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2093)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2372)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1722)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2333)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.View.dispatchPointerEvent(View.java:8742)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4136)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4002)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3557)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3610)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3576)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3693)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3584)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3750)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3557)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3610)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3576)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3584)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3557)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5823)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5797)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5768)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5913)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.os.MessageQueue.nativePollOnce(Native Method)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.os.MessageQueue.next(MessageQueue.java:143)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.os.Looper.loop(Looper.java:195)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at android.app.ActivityThread.main(ActivityThread.java:5373)
      11-26 12:56:24.868 31769-31769/com.example.keegan.securetipping E/MessageQueue-JNI:     at java.lang.reflect.Method.invoke(Native Method)
      11-26 12:56:24.869 31769-31769/com.example.keegan.securetipping D/AndroidRuntime: Shutting down VM
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime: FATAL EXCEPTION: main
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime: Process: com.example.keegan.securetipping, PID: 31769
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' on a null object reference
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at com.example.keegan.securetipping.HistoryFragment.refreshHistory(HistoryFragment.java:88)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at com.example.keegan.securetipping.MainActivity$1.onPageSelected(MainActivity.java:54)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1794)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:569)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:553)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2102)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.View.dispatchTouchEvent(View.java:8547)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2400)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2093)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2372)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1722)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2333)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.View.dispatchPointerEvent(View.java:8742)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4136)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4002)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3557)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3610)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3576)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3693)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3584)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3750)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3557)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3610)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3576)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3584)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3557)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5823)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5797)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5768)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5913)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.os.MessageQueue.nativePollOnce(Native Method)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.os.MessageQueue.next(MessageQueue.java:143)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:195)
      11-26 12:56:24.874 31769-31769/com.example.keegan.securetipping E/AndroidRuntime:   at android.app.ActivityThread.main(Act
      

      这是来自我的HistoryFragment类的代码位,它抛出异常。我只是调用getApplicationContext以获取此示例的错误。通常,错误将在我的FetchHistoryEntries AsyncTask中抛出。

      /**
       * Refreshes the listView in the history fragment with data from the history database.
       */
      public void refreshHistory(){
      
          getActivity().getApplicationContext();
          new FetchHistoryEntries(getActivity()).execute();
      }
      

      我的历史片段只包含一个listView。这是它的XML。

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >
      
      <ListView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/history_listview"/>
      

2 个答案:

答案 0 :(得分:0)

当一个活动被销毁时,你会丢失你正在使用的上下文,这就是你的应用程序抛出异常的原因,因为你在销毁抛出空指针异常的活动后调用getActivity(),以避免这种情况您应该将refreshHistory更改为以下内容:

public void refreshHistory(){
    if (isAdded()) {
        getActivity().getApplicationContext();
        new FetchHistoryEntries(getActivity()).execute();
    }
}
当片段附加到活动且活动不为空时,

isAdded()返回true。

您还应考虑在销毁活动时取消AsyncTask,您可以执行以下操作:首先定义与此类FetchHistoryEntries task;相同类型的AsyncTask的全局变量。然后,像执行全局任务一样执行这个

task = new FetchHistoryEntries(getActivity()).execute;

最后,在你的onDestroy重写方法中取消你的任务:

if (task != null) {
    task.cancel(true);
}

希望这对你有所帮助,祝你好运

答案 1 :(得分:0)

我实际上是通过切换到与FragmentPagerAdapter相对的FragmentStatePagerAdapter来解决这个问题。状态分页器适配器可以更容易地保存和恢复片段状态,这就是我的问题所在。这个要点有一个方便的例子。 https://gist.github.com/nesquena/c715c9b22fb873b1d259