片段管理器中的NullPointerException

时间:2015-09-15 09:53:57

标签: android android-fragments android-support-library

任何人都可以帮我解决这个例外吗? 这是片段管理器的代码以及如何在活动中显示我的片段

private void selectItem(int position) {
    Fragment fragment = null;
    // minus 1 because we have header that has 0 position
    if (position < 1) { // because we have header, we skip clicking on it
        return;
    }
    switch (position) {
        case 2:
            fragment = new TextViewsFragment();
            break;
        case 3:
            //fragment = new ProfilFragment();
            break;
        case 4:
            //fragment = new AttendanceFragment();
            break;
        case 5:
            //fragment = new PermitFragment();
            break;
        case 6:
            //Logout();
            break;
        default:
            break;
    }
    commitFragment(fragment);

    mDrawerList.setItemChecked(position, true);
    setTitle(mDrawerItems.get(position - 1).getTitle());
    mDrawerLayout.closeDrawer(mDrawerList);
}
private class CommitFragmentRunnable implements Runnable {

    private Fragment fragment;

    public CommitFragmentRunnable(Fragment fragment) {
        this.fragment = fragment;
    }

    @Override
    public void run() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment).commit();
    }
}

public void commitFragment(Fragment fragment) {
    // Using Handler class to avoid lagging while
    // committing fragment in same time as closing
    // navigation drawer
    mHandler.post(new CommitFragmentRunnable(fragment));
}

.replace(R.id.content_frame, fragment).commit();

的异常点

获得异常:

java.lang.NullPointerException
            at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:416)
            at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:451)
            at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:443)
            at com.csform.android.uiapptemplate.LeftMenusActivity$CommitFragmentRunnable.run(LeftMenusActivity.java:221)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5268)
            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:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)

我尝试通过选择左侧菜单将一个片段调用到我的活动中,但它会获得异常空指针。

1 个答案:

答案 0 :(得分:1)

参考Mamata Gelanee的评论:

private FragmentManager fragmentManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
      ......
      fragmentManager = getSupportFragmentManager();
    }

然后在你的runnable中:

    @Override
    public void run() {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment).commit();
    }