0

我正在尝试使用上下文传输将上下文导入此 android 文件。但是,如果我的消息包含“aakash”,则此代码会意外终止,并给出代码后显示的错误。

我该怎么办 ?

    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();
                                    } 

                                }
                                }
                            }
                        }

    }

错误如下:

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)

将 mContext 更改为上下文后,新代码出现空指针异常。代码是

    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;   

                                    } 

                                }
                                }
                            }
                        }
            }
    }
4

1 回答 1

3

采用

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

代替

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

使用当前BroadcastReceiver Context显示 ToastBroadcastReceiver不是任何旧的活动或服务上下文

于 2012-11-26T09:40:26.363 回答