在android中创建活动时如何排除NPE故障?

时间:2014-12-30 05:58:21

标签: android android-activity nullpointerexception

当我在我的应用程序中启动活动时,我正在获得NPE。当我启动手机并开始调试时,它不会马上发生。在将几十个新版本推送到我的手机后,它最终每次都会因此错误而崩溃。在激活错误的活动之前,我可以先打开一些其他活动来暂时补救。

任何可能导致此问题的想法?这是一个有点复杂的Activity,带有DrawerLayout,以及一个包含带有ListView的SwipeRefreshLayout的片段。

异常

java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.NullPointerException: Attempt to read from field 'boolean android.support.v4.app.BackStackRecord.mAddToBackStack' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)

活动代码

public class TastingsActivity extends BaseActivity implements TastingListFragment.Callbacks {
    public static final String TAG = TastingsActivity.class.getSimpleName();

    private TastingListFragment mTastingListFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_tastings);

        //create fragment
        // Check that the activity is using the layout version with
        // the fragment_container FrameLayout
        if (findViewById(R.id.container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create a new Fragment to be placed in the activity layout
            TastingListFragment fragment = new TastingListFragment();

            // In case this activity was started with special instructions from an
            // Intent, pass the Intent's extras to the fragment as arguments
            fragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, fragment).commit();
        }
    }

    @Override
    protected int getSelfNavDrawerItem() {
        return NAVDRAWER_ITEM_TASTINGS;
    }

    //================================================================================
    // Handlers
    //================================================================================

    @Override
    public void onTastingSelected(Tasting tasting) {
        Intent intent = new Intent(this, TastingActivity.class);
        intent.putExtra(TastingDetailsFragment.EXTRA_TASTING_ID, tasting.getId());
        startActivity(intent);
    }
}

0 个答案:

没有答案