以下代码扫描传入的消息以获取代码(zp),并在其包含消息时发出警报。 但是当短信到达时,程序会强制关闭。任何PLZ可以帮助我解决这个问题吗? 我还在清单文件中点了sms reciver和permission ..
公共类Sms扩展了BroadcastReceiver {
private static final Context context = null;
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
if(message.matches("zp"))
{
Toast.makeText(context, "code received", Toast.LENGTH_SHORT).show();
}
} // end for loop
} // bundle is null
} catch (Exception e) {
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
}
}
}
这是我的logcat
12-14 06:44:21.464:W / Trace(12235):来自nativeGetEnabledTags的意外值:0 12-14 06:44:21.643:D / AndroidRuntime(12235):关闭VM 12-14 06:44:21.643:W / dalvikvm(12235):threadid = 1:线程退出未捕获异常(组= 0x40a70930) 12-14 06:44:21.653:E / AndroidRuntime(12235):致命异常:主要 12-14 06:44:21.653:E / AndroidRuntime(12235):java.lang.RuntimeException:无法启动接收器com.christmas.Sms:java.lang.NullPointerException 12-14 06:44:21.653:E / AndroidRuntime(12235):在android.app.ActivityThread.handleReceiver(ActivityThread.java:2383) 12-14 06:44:21.653:E / AndroidRuntime(12235):在android.app.ActivityThread.access $ 1500(ActivityThread.java:141) 12-14 06:44:21.653:E / AndroidRuntime(12235):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1310) 12-14 06:44:21.653:E / AndroidRuntime(12235):在android.os.Handler.dispatchMessage(Handler.java:99) 12-14 06:44:21.653:E / AndroidRuntime(12235):在android.os.Looper.loop(Looper.java:137) 12-14 06:44:21.653:E / AndroidRuntime(12235):在android.app.ActivityThread.main(ActivityThread.java:5039) 12-14 06:44:21.653:E / AndroidRuntime(12235):at java.lang.reflect.Method.invokeNative(Native Method) 12-14 06:44:21.653:E / AndroidRuntime(12235):at java.lang.reflect.Method.invoke(Method.java:511) 12-14 06:44:21.653:E / AndroidRuntime(12235):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 12-14 06:44:21.653:E / AndroidRuntime(12235):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 12-14 06:44:21.653:E / AndroidRuntime(12235):at dalvik.system.NativeStart.main(Native Method) 12-14 06:44:21.653:E / AndroidRuntime(12235):引起:java.lang.NullPointerException 12-14 06:44:21.653:E / AndroidRuntime(12235):在android.widget.Toast。(Toast.java:92) 12-14 06:44:21.653:E / AndroidRuntime(12235):在android.widget.Toast.makeText(Toast.java:238) 12-14 06:44:21.653:E / AndroidRuntime(12235):at com.christmas.Sms.onReceive(Sms.java:60) 12-14 06:44:21.653:E / AndroidRuntime(12235):在android.app.ActivityThread.handleReceiver(ActivityThread.java:2376) 12-14 06:44:21.653:E / AndroidRuntime(12235):... 10 more
答案 0 :(得分:1)
onReceive方法有参数Context arg0所以Toast代码应该是:
Toast.makeText(arg0, "code received", Toast.LENGTH_SHORT).show();