SMS接收器仅在某个活动打开时才有效

时间:2013-08-29 13:09:41

标签: java android sms gsm

我创建了一个应用程序,它使用BroadcastReceiver类在应用程序中接收SMS消息,以便在收到某个文本消息时更改和控制应用程序中的不同内容。我最近在应用程序中添加了一个新活动,现在的问题是,如果应用程序收到SMS消息,它将崩溃,除非新活动已经启动。谁能想到为什么会这样? 谢谢

这是崩溃错误的logcat:

 07-31 15:51:50.135: E/AndroidRuntime(779): FATAL EXCEPTION: main
 07-31 15:51:50.135: E/AndroidRuntime(779): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.becatech.gsmzonecontroller/com.becatech.gsmzonecontroller.MainActivity}: java.lang.NullPointerException
 07-31 15:51:50.135: E/AndroidRuntime(779):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at android.os.Handler.dispatchMessage(Handler.java:99)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at android.os.Looper.loop(Looper.java:137)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at android.app.ActivityThread.main(ActivityThread.java:4921)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at java.lang.reflect.Method.invokeNative(Native Method)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at java.lang.reflect.Method.invoke(Method.java:511)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at dalvik.system.NativeStart.main(Native Method)
 07-31 15:51:50.135: E/AndroidRuntime(779): Caused by: java.lang.NullPointerException
 07-31 15:51:50.135: E/AndroidRuntime(779):     at com.becatech.gsmzonecontroller.MainActivity.onCreate(MainActivity.java:66)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at android.app.Activity.performCreate(Activity.java:5206)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
 07-31 15:51:50.135: E/AndroidRuntime(779):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)

2 个答案:

答案 0 :(得分:1)

您的主要活动

第66行有一个nullPointerException

答案 1 :(得分:0)

在接收器中执行此操作以启动活动并发送消息:

Intent intent = new Intent(context, Your_Activity.class);
                intent.putExtra("msg", message_received);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);

在您的活动中,您可以检查消息并更改切换按钮