BroadcastReceiver’s code is a separate unit SMSReceiver.java. The Receiver declared in the Manifest with an SMS Receive Intent Filter. So it is “always on”. When the application is running or just in the list of last 8 application it’s working fine when get incoming SMS. But later, when application is deleted from memory, and only receiver stands by, incoming SMS due to error. The fact that I use in receiver’s code Sharedpreferences. When I get some information from received SMS I should save it in preferences for loading by main Activity later.
LogCat shows me a string with error.
public class SmsReceiver extends BroadcastReceiver
{
public static SharedPreferences mStatePrefs;
@Override
public void onReceive(Context context, Intent intent)
{
//... some code
SharedPreferences.Editor ed = mStatePrefs.edit(); //HERE I GET AN EXCEPTION
//... some code
}
}
LogCat log..
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): FATAL EXCEPTION: main
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): java.lang.RuntimeException: Unable to start receiver com.example.android.MyApplication.SmsReceiver: java.lang.NullPointerException
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2034)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.app.ActivityThread.access$2400(ActivityThread.java:132)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1098)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.os.Looper.loop(Looper.java:143)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.app.ActivityThread.main(ActivityThread.java:4268)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at java.lang.reflect.Method.invoke(Method.java:507)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at dalvik.system.NativeStart.main(Native Method)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): Caused by: java.lang.NullPointerException
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at com.example.android.MyApplication.SmsReceiver.onReceive(SmsReceiver.java:198)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2019)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): ... 10 more
So it works fine when app is active or in memory. But when the receiver was left alone, the error has occurred. So what I need to do, to fix this problem? It’s a block bug for my app, this receiver is main solution, a sense of program. It must work always and save information in preferences.
You never get a
SharedPreferenceinstance for mStatePrefs. Looking at just the code you show, mStatePrefs would still be null.Try: