Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6908101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:31:09+00:00 2026-05-27T08:31:09+00:00

BroadcastReceiver’s code is a separate unit SMSReceiver.java. The Receiver declared in the Manifest with

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:31:10+00:00Added an answer on May 27, 2026 at 8:31 am

    You never get a SharedPreference instance for mStatePrefs. Looking at just the code you show, mStatePrefs would still be null.

    Try:

    mStatePrefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor ed = mStatePrefs.edit();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an BroadcastReceiver for handling new or modified applications: <receiver android:name=.PackageHandler > <intent-filter>
I'm trying to receive SMS using broadcast receiver. MySMSReceiver : import android.content.BroadcastReceiver; import android.content.Context;
App A has this BroadcastReceiver in its manifest (within <application>): And this receiver: public
I have a class that extends BroadcastReceiver that reads new sms public class SmsReceiver
I am using BroadcastReceiver class for receiving the sms. My main class code is:
Let's say a BroadcastReceiver is declared in the manifest. It has run through onReceive()
My app uses a BroadcastReceiver to get sms in this way: SmsBR.java public class
I was wondering is it possible to register a broadcast receiver to receive two
I try to develope a simple Broadcastreceiver. Here's my code: public class MyClass extends
I have the following broadcast receiver: public class MyRingModeReceiver extends BroadcastReceiver { @Override public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.