最近在学习Slidingmenu+fragment的框架开发,发现个问题。按下home键返回桌面,再重新进入程序时报错。
在部分小内存手机上经常出现这个空指针问题,同时在开发者选项中如果选中了不保留活动选项,也会出现这种问题。
Logcat中出错的相关信息:
XXXX表示包含个人信息,使用X代替.
java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.xxx/xxx.xxx.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivityHelper.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2406)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
at android.app.ActivityThread.access$1200(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5539)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivityHelper.findViewById(int)' on a null object reference
at com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity.findViewById(SlidingFragmentActivity.java:42)
at xxx.xxx.fragment.ssl.XXXFragment.onCreate(XXXFragment.java:283)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1879)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:215)
at com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity.onCreate(SlidingFragmentActivity.java:20)
at xxx.xxx.activity.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6013)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
at android.app.ActivityThread.access$1200(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5539)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
一开始以为是slidingmenu的findViewById出问题了,查了半天没有发现任何情况,后来继续看xxxFragment.class的类283行才发现原因:
在该类中,Textview的初始化赋值写到了onCreate里面,实际是应该写到onCreateView中的。修改后没有发现报错的情况。