如果在接收msn时它在后台运行,Android SmsReceiver会使我的应用程序崩溃

时间:2014-06-27 11:27:15

标签: android android-activity android-fragments broadcastreceiver android-dialog

如果我在应用程序内,我可以检查是否收到短信然后播放视频。但如果我按下主页按钮并且应用程序在后台运行,那么如果收到短信,应用程序将崩溃。

感谢您的帮助。

**我的代码:在**公共类中,SmsReceiver扩展了BroadcastReceiver {

...
         if (run this if corrected sms is received  ){

                    //Pause Video in PlayVideoActivity 
                    //PlayVideoActivity.mVideoView.pause();

                    /** Go to Dialog box 2 for Confirmation **/
                    PlayVideoActivity.DialogTextComfirmRun();

}
...

错误短信

06-27 11:05:30.611: D/AndroidRuntime(707): Shutting down VM
06-27 11:05:30.631: W/dalvikvm(707): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-27 11:05:30.671: E/AndroidRuntime(707): FATAL EXCEPTION: main
06-27 11:05:30.671: E/AndroidRuntime(707): java.lang.RuntimeException: Unable to start receiver com.example.brazilapps.SmsReceiver: java.lang.NullPointerException
06-27 11:05:30.671: E/AndroidRuntime(707):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
06-27 11:05:30.671: E/AndroidRuntime(707):  at android.app.ActivityThread.access$2400(ActivityThread.java:117)
06-27 11:05:30.671: E/AndroidRuntime(707):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
06-27 11:05:30.671: E/AndroidRuntime(707):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 11:05:30.671: E/AndroidRuntime(707):  at android.os.Looper.loop(Looper.java:123)
06-27 11:05:30.671: E/AndroidRuntime(707):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-27 11:05:30.671: E/AndroidRuntime(707):  at java.lang.reflect.Method.invokeNative(Native Method)
06-27 11:05:30.671: E/AndroidRuntime(707):  at java.lang.reflect.Method.invoke(Method.java:507)
06-27 11:05:30.671: E/AndroidRuntime(707):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-27 11:05:30.671: E/AndroidRuntime(707):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-27 11:05:30.671: E/AndroidRuntime(707):  at dalvik.system.NativeStart.main(Native Method)
06-27 11:05:30.671: E/AndroidRuntime(707): Caused by: java.lang.NullPointerException
06-27 11:05:30.671: E/AndroidRuntime(707):  at android.widget.Toast.<init>(Toast.java:89)
06-27 11:05:30.671: E/AndroidRuntime(707):  at android.widget.Toast.makeText(Toast.java:231)
06-27 11:05:30.671: E/AndroidRuntime(707):  at com.example.brazilapps.PlayVideoActivity.DialogTextComfirmRun(PlayVideoActivity.java:302)
06-27 11:05:30.671: E/AndroidRuntime(707):  at com.example.brazilapps.SmsReceiver.onReceive(SmsReceiver.java:81)
06-27 11:05:30.671: E/AndroidRuntime(707):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
06-27 11:05:30.671: E/AndroidRuntime(707):  ... 10 more

有人建议我:提示:在你的接收器类文件中打开一个对话框活动 - 但如果应用程序在后台运行,我不知道如何打开对话框。< / p>

任何帮助都很棒。再次感谢

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码从BroadcastReceiver启动活动:

puboic void onReceive(Context context, Intent intent){
    ...
    if (run this if corrected sms is received ){
        // context can be the context object in the argument of onReceive method
        Intent i = new Intent(context, PlayVideoActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // use i.puExtra() to pass any data
        context.startActivity(i);
    }
}

启动Activity后,您可以从Activity打开对话框或执行任何需要用户界面的操作。

logcat显示空指针异常。因此,您还应该为PlayVideoActivity.DialogTextComfirmRun()方法添加代码,以找出当前方法的错误。