Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 活动中onStart内部的NullPointerException_Android_Android Activity_Nullpointerexception - Fatal编程技术网

Android 活动中onStart内部的NullPointerException

Android 活动中onStart内部的NullPointerException,android,android-activity,nullpointerexception,Android,Android Activity,Nullpointerexception,我在扩展AppCompatActivity的活动中偶尔遇到空指针异常。请注意,这种情况偶尔会发生,所以剩下的时间代码工作正常。活动中的onStart函数几乎为空,只调用下面的超级函数 @Override protected void onStart() { super.onStart(); //**<---- line 242** } 一次创建 // Checks if fragment has already been added, otherwise add a new

我在扩展AppCompatActivity的活动中偶尔遇到空指针异常。请注意,这种情况偶尔会发生,所以剩下的时间代码工作正常。活动中的onStart函数几乎为空,只调用下面的超级函数

@Override
protected void onStart() {
    super.onStart();  //**<---- line 242**
}
一次创建

// Checks if fragment has already been added, otherwise add a new
    mFragment = getSupportFragmentManager().findFragmentByTag(tag);

    if (mFragment != null) {
        Utils.bringFragmentToTop(getSupportFragmentManager(), mFragment);
        return;
    }

    if(isDialog) {
        try {
            Fragment fragment = (Fragment) fragmentClass.newInstance();
            if (null != b) {
                fragment.setArguments(b);
            }
            ((DialogFragment) fragment).show(getSupportFragmentManager(), tag);
            getSupportFragmentManager().executePendingTransactions();
            return;
        } catch (Exception e) {}

    }

    Utils.startFragment(getSupportFragmentManager(), fragmentClass, R.id.generic_frame, tag, b, addToStack);
下面是Utils.startFragment代码

public static Fragment startFragment(FragmentManager fm, Class c, int containerid, String tag, Bundle args, boolean addToStack) {
    if(TextUtils.isEmpty(tag)) {
        tag = getClassTag(c);
    }

    Fragment fragment = null;
    if(!TextUtils.isEmpty(tag)) {
        //if fragment is in backstack, we use it and return
        if(fm.popBackStackImmediate (tag, 0)) {
            return null;
        }

        //We see if Fragment manager has it
        fragment = fm.findFragmentByTag(tag);

        // if the fragment is currently visible, don't add it to stack or commit any trasaction
        if(null != fragment && fragment.isVisible()) {
            return fragment;
        }

    }

    if (fragment == null) {
        try {
            fragment = (Fragment) c.newInstance();
            if(null != args) {
                fragment.setArguments(args);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return fragment;
        }
    }

    if(fragment instanceof DialogFragment) {
        DialogFragment df = (DialogFragment) fragment;

        // we do getDialog to check if setShowsDialog(false) was called on it (if dialog is used as both generic and dialog
        //if(null != df.getDialog()) {
            ((DialogFragment) fragment).show(fm, tag);
            fm.executePendingTransactions();
        return fragment;
        //}
    }

    FragmentTransaction transaction = fm.beginTransaction();
    if(fragment.isAdded()) {
        transaction.show(fragment);
    }
    else
        transaction.replace(containerid, fragment, tag);

    if(addToStack)
        transaction.addToBackStack(tag);

    if(null != args && args.getBoolean("commitAllowingStateLoss", false)) {
        Log.d(TAG, "commitAllowingStateLoss");
        transaction.commitAllowingStateLoss();
    }
    else
        transaction.commit();

    return fragment;

}

有什么想法吗

我可以知道你在onCreate里面有什么吗?把你的活动的完整代码粘贴到上面的onCreate代码中。谢谢这里的答案有什么提示吗?你认为写上述代码的人不知道空指针异常吗?
public static Fragment startFragment(FragmentManager fm, Class c, int containerid, String tag, Bundle args, boolean addToStack) {
    if(TextUtils.isEmpty(tag)) {
        tag = getClassTag(c);
    }

    Fragment fragment = null;
    if(!TextUtils.isEmpty(tag)) {
        //if fragment is in backstack, we use it and return
        if(fm.popBackStackImmediate (tag, 0)) {
            return null;
        }

        //We see if Fragment manager has it
        fragment = fm.findFragmentByTag(tag);

        // if the fragment is currently visible, don't add it to stack or commit any trasaction
        if(null != fragment && fragment.isVisible()) {
            return fragment;
        }

    }

    if (fragment == null) {
        try {
            fragment = (Fragment) c.newInstance();
            if(null != args) {
                fragment.setArguments(args);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return fragment;
        }
    }

    if(fragment instanceof DialogFragment) {
        DialogFragment df = (DialogFragment) fragment;

        // we do getDialog to check if setShowsDialog(false) was called on it (if dialog is used as both generic and dialog
        //if(null != df.getDialog()) {
            ((DialogFragment) fragment).show(fm, tag);
            fm.executePendingTransactions();
        return fragment;
        //}
    }

    FragmentTransaction transaction = fm.beginTransaction();
    if(fragment.isAdded()) {
        transaction.show(fragment);
    }
    else
        transaction.replace(containerid, fragment, tag);

    if(addToStack)
        transaction.addToBackStack(tag);

    if(null != args && args.getBoolean("commitAllowingStateLoss", false)) {
        Log.d(TAG, "commitAllowingStateLoss");
        transaction.commitAllowingStateLoss();
    }
    else
        transaction.commit();

    return fragment;

}