我正在尝试在 android 中学习片段,并在onPause()
活动暂停时尝试在 Activity 的方法中用另一个片段替换片段。
我的 onPause 方法代码是:-
public void onPause(){
super.onPause();
System.out.println("ActivityA-onPause()-1");
ArticleListFragment temp=(ArticleListFragment) getFragmentManager().findFragmentById(R.id.article_list_fragment);
System.out.println("ActivityA-onPause()-2");
if(!temp.isAdded()){ //problem
System.out.println("ActivityA-onPause()-3");
fragmentTransaction.replace(R.id.activity_a, articleReaderFragment, new String("readerFragment"));
System.out.println("ActivityA-onPause()-4");
}
System.out.println("ActivityA-onPause()-5");
}
当我在设备上执行应用程序时,onPause() 执行但仅打印到“Activity-onPause()-2”但不打印“Activity-onPause()-3”。
我得到错误日志: -
06-13 00:25:10.946: I/System.out(17974): ActivityA-onPause()-1
06-13 00:25:10.946: I/System.out(17974): ActivityA-onPause()-2
06-13 00:25:10.946: D/AndroidRuntime(17974): Shutting down VM
06-13 00:25:10.946: W/dalvikvm(17974): threadid=1: thread exiting with uncaught exception (group=0xb4e04908)
06-13 00:25:10.961: E/AndroidRuntime(17974): FATAL EXCEPTION: main
06-13 00:25:10.961: E/AndroidRuntime(17974): java.lang.RuntimeException: Unable to pause activity {com.pravesh.fragments/com.pravesh.fragments.ActivityA}: java.lang.NullPointerException
06-13 00:25:10.961: E/AndroidRuntime(17974): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3016)
06-13 00:25:10.961: E/AndroidRuntime(17974): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2971)
06-13 00:25:10.961: E/AndroidRuntime(17974): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2949)
06-13 00:25:10.961: E/AndroidRuntime(17974): at android.app.ActivityThread.access$800(ActivityThread.java:141)
06-13 00:25:10.961: E/AndroidRuntime(17974): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
06-13 00:25:10.961: E/AndroidRuntime(17974): at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 00:25:10.961: E/AndroidRuntime(17974): at android.os.Looper.loop(Looper.java:137)
06-13 00:25:10.961: E/AndroidRuntime(17974): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-13 00:25:10.961: E/AndroidRuntime(17974): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 00:25:10.961: E/AndroidRuntime(17974): at java.lang.reflect.Method.invoke(Method.java:511)
06-13 00:25:10.961: E/AndroidRuntime(17974): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-13 00:25:10.961: E/AndroidRuntime(17974): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-13 00:25:10.961: E/AndroidRuntime(17974): at dalvik.system.NativeStart.main(Native Method)
06-13 00:25:10.961: E/AndroidRuntime(17974): Caused by: java.lang.NullPointerException
06-13 00:25:10.961: E/AndroidRuntime(17974): at com.pravesh.fragments.ActivityA.onPause(ActivityA.java:37)
06-13 00:25:10.961: E/AndroidRuntime(17974): at android.app.Activity.performPause(Activity.java:5206)
06-13 00:25:10.961: E/AndroidRuntime(17974): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1226)
06-13 00:25:10.961: E/AndroidRuntime(17974): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3002)
06-13 00:25:10.961: E/AndroidRuntime(17974): ... 12 more
我能理解的是它给出了一个空指针异常,但我不知道为什么。