我在扩展AppCompatActivity的Activity中得到偶尔的空指针异常。请注意,它偶尔会发生,因此剩下的时间代码工作正常。活动中的onStart函数非常空,只调用super函数,如下所示。
@Override
protected void onStart() {
super.onStart(); //**<---- line 242**
}
这是crashlog
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.korneto.android/com.korneto.android.activities.GenericActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1654)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1670)
at android.app.ActivityThread.access$1500(ActivityThread.java:118)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3704)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v4.app.BackStackRecord.run(SourceFile:714)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(SourceFile:1501)
at android.support.v4.app.FragmentActivity.onStart(SourceFile:551)
at com.korneto.android.activities.GenericActivity.onStart(SourceFile:242)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3791)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1627)
的OnCreate
// 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;
}
有什么想法吗?