我的Android应用程序工作正常。但是当我尝试清理内存然后尝试打开时,它崩溃并在几个地方给出NullPointerException。我知道当用户清除内存时,android会清除所有变量和sharedPreferences。现在我的问题是我们可以知道用户何时清除手机设置中的RAM内存。
这是Logcat
05-29 15:34:19.813: E/AndroidRuntime(4932): FATAL EXCEPTION: main
05-29 15:34:19.813: E/AndroidRuntime(4932): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.softwaysolutions.rcommunication/com.softwaysolutions.rcommunication.MainActivity}: java.lang.NullPointerException
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.app.ActivityThread.access$700(ActivityThread.java:139)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.os.Looper.loop(Looper.java:137)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.app.ActivityThread.main(ActivityThread.java:4918)
05-29 15:34:19.813: E/AndroidRuntime(4932): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 15:34:19.813: E/AndroidRuntime(4932): at java.lang.reflect.Method.invoke(Method.java:511)
05-29 15:34:19.813: E/AndroidRuntime(4932): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-29 15:34:19.813: E/AndroidRuntime(4932): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-29 15:34:19.813: E/AndroidRuntime(4932): at dalvik.system.NativeStart.main(Native Method)
05-29 15:34:19.813: E/AndroidRuntime(4932): Caused by: java.lang.NullPointerException
05-29 15:34:19.813: E/AndroidRuntime(4932): at com.softwaysolutions.rcommunication.NowPlayingFragment.startMyAACService(NowPlayingFragment.java:378)
05-29 15:34:19.813: E/AndroidRuntime(4932): at com.softwaysolutions.rcommunication.NowPlayingFragment.onCreateView(NowPlayingFragment.java:112)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:566)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.app.Activity.performStart(Activity.java:5058)
05-29 15:34:19.813: E/AndroidRuntime(4932): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
05-29 15:34:19.813: E/AndroidRuntime(4932): ... 11 more
提前致谢。
答案 0 :(得分:1)
我已经在那里查了一下,事情是我试图访问全局 值
您需要将所有数据保存在onSavedInstanceState
中,并在onRestoreInstanceState
上恢复。
答案 1 :(得分:1)
根据您的异常堆栈跟踪:
操作系统从内存中清除了您的活动,片段内的对象变为NULL,
确保使用onSavedInstanceState来保存实例,并使用onRestoreInstanceState来恢复实例。