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 8887027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:42:16+00:00 2026-06-14T21:42:16+00:00

I am trying to import a context into this android file using the Context

  • 0

I am trying to import a context into this android file using the Context transfer. But if my message contains “aakash”, then this code unexpectedly terminates giving the error as shown after the code.

What do I do ?

    public class messagebody extends BroadcastReceiver{

             Context mContext;
             public messagebody(Context context){
                   this.mContext = context;
                   Log.i(TAG, "yayyyy: " );
              }
             public messagebody(){

          }


            private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
            private static final String TAG = "SMSBroadcastReceiver";

            @Override
            public void onReceive(Context context, Intent intent) {
                 Log.i(TAG, "Intent recieved: " + intent.getAction());

                    if (intent.getAction().equals(SMS_RECEIVED)) {
                        Bundle bundle = intent.getExtras();
                        if (bundle != null) {
                            Object[] pdus = (Object[])bundle.get("pdus");
                            final SmsMessage[] messages = new SmsMessage[pdus.length];
                            for (int i = 0; i < pdus.length; i++) {
                                messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                            }
                            if (messages.length > -1) {
                               // Toast.makeText(context, "Message recieved: " + messages[0].getMessageBody(), 7000).show();
                                if(messages[0].getMessageBody().contains("aakash"))
                                {
                                    /*AudioManager am = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
                                    switch (am.getRingerMode()) {   
                                        case AudioManager.RINGER_MODE_NORMAL:   
                                            //Log.i("MyApp","NORMAL mode");
                                            am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                                            Toast toast2=Toast.makeText(mContext, "Vibrate", Toast.LENGTH_LONG);  
                                            toast2.show();
                                            break;                
                                        case AudioManager.RINGER_MODE_VIBRATE:   
                                            //Log.i("MyApp","VIBRATE mode");
                                            am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                                            Toast toast1=Toast.makeText(mContext, "Normal", Toast.LENGTH_LONG);  
                                            toast1.show();
                                            break;   */
                                    Toast toast1=Toast.makeText(mContext, "Normal", Toast.LENGTH_LONG);  
                                    toast1.show();
                                    } 

                                }
                                }
                            }
                        }

    }

The error is as follows :

11-26 15:03:43.150: E/AndroidRuntime(2056): FATAL EXCEPTION: main
11-26 15:03:43.150: E/AndroidRuntime(2056): java.lang.RuntimeException: Unable to start receiver com.example.messages.messagebody: java.lang.NullPointerException
11-26 15:03:43.150: E/AndroidRuntime(2056):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2236)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at android.app.ActivityThread.access$1500(ActivityThread.java:130)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at android.os.Looper.loop(Looper.java:137)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at java.lang.reflect.Method.invokeNative(Native Method)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at java.lang.reflect.Method.invoke(Method.java:511)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at dalvik.system.NativeStart.main(Native Method)
11-26 15:03:43.150: E/AndroidRuntime(2056): Caused by: java.lang.NullPointerException
11-26 15:03:43.150: E/AndroidRuntime(2056):     at android.widget.Toast.<init>(Toast.java:92)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at android.widget.Toast.makeText(Toast.java:238)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at com.example.messages.messagebody.onReceive(messagebody.java:68)
11-26 15:03:43.150: E/AndroidRuntime(2056):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2229)

With the mContext changed to context I get a null pointer exception with the new code. The code is

    package com.example.messages;
import com.example.messages.R;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import android.media.AudioManager;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

    public class messagebody extends BroadcastReceiver{

             Context mContext;
             public messagebody(Context context){
                   this.mContext = context;
                   Log.i(TAG, "yayyyy: " );
              }
             public messagebody(){

          }


            private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
            private static final String TAG = "SMSBroadcastReceiver";

            @Override
            public void onReceive(Context context, Intent intent) {
                 Log.i(TAG, "Intent recieved: " + intent.getAction());

                    if (intent.getAction().equals(SMS_RECEIVED)) {
                        Bundle bundle = intent.getExtras();
                        if (bundle != null) {
                            Object[] pdus = (Object[])bundle.get("pdus");
                            final SmsMessage[] messages = new SmsMessage[pdus.length];
                            for (int i = 0; i < pdus.length; i++) {
                                messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                            }
                            if (messages.length > -1) {
                               // Toast.makeText(context, "Message recieved: " + messages[0].getMessageBody(), 7000).show();
                                if(messages[0].getMessageBody().contains("aakash"))
                                {
                                    Toast toast3=Toast.makeText(context, "Normal", Toast.LENGTH_LONG);  
                                    toast3.show();
                                    AudioManager am = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
                                    switch (am.getRingerMode()) {   
                                        case AudioManager.RINGER_MODE_NORMAL:   
                                            //Log.i("MyApp","NORMAL mode");
                                            am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                                            Toast toast2=Toast.makeText(context, "Vibrate", Toast.LENGTH_LONG);  
                                            toast2.show();
                                            break;                
                                        case AudioManager.RINGER_MODE_VIBRATE:   
                                            //Log.i("MyApp","VIBRATE mode");
                                            am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                                            Toast toast1=Toast.makeText(context, "Normal", Toast.LENGTH_LONG);  
                                            toast1.show();
                                            break;   

                                    } 

                                }
                                }
                            }
                        }
            }
    }
  • 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-06-14T21:42:18+00:00Added an answer on June 14, 2026 at 9:42 pm

    use

    Toast toast1=Toast.makeText(context, "Normal", Toast.LENGTH_LONG);  
     toast1.show();
    

    instead of

      Toast toast1=Toast.makeText(mContext, "Normal", Toast.LENGTH_LONG);  
      toast1.show();
    

    use current BroadcastReceiver Context to show Toast from BroadcastReceiver instead of any old Activity or Service Context

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to import wordpress content into liferay 6.1 using the wordpress importer
I am trying to import the MoPub sdk into eclipse, but I am getting
I'm trying to import an XML file that contains a list of points for
I am trying this import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream;
I'm currently trying to import about 10000 rows (from a CSV file) into an
I'm trying to import our SVN repository into Git. When I run either this
I am trying to import a set of data into a CoreData persistentStore. This
I'm trying to implement an Android spinner in my app but am running into
I'm trying to integrate abs into my app, but I keep getting this error
I am trying to make an alarm application, and found this code online. But

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.