通过BroadcastReceiver启动电话呼叫活动会使呼叫应用程序崩溃

时间:2013-11-29 20:14:16

标签: android broadcastreceiver

在我的BroadcastReceiver的onReceive()函数中,我调用以下代码拨打电话,无需用户干预,并使用以下代码:

    Intent intentcall = new Intent();
    intentcall.setAction(Intent.ACTION_CALL);
    intentcall.setData(Uri.parse("tel:" + phoneNumber)); 
    startActivity(intentcall);

上面的代码放在调用活动的onCreate函数中时起作用,但在BroadcastReceiver的onReceive()函数中失败。 BroadcastReceiver是调用活动中的内部类。

以下是错误:

    11-29 15:15:40.822: E/AndroidRuntime(6354): FATAL EXCEPTION: main 11-29 15:15:40.822: E/AndroidRuntime(6354): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.NEW_OUTGOING_CALL flg=0x10000010 (has extras) } in com.ensil.vastusyscontrolunit.OutgoingReceiver@43d3d4e8 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:781) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.os.Handler.handleCallback(Handler.java:725) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.os.Handler.dispatchMessage(Handler.java:92) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.os.Looper.loop(Looper.java:137) 11-29 15:15:40.822: E/AndroidRuntime(6354): at android.app.ActivityThread.main(ActivityThread.java:5328) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at java.lang.reflect.Method.invokeNative(Native Method) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at java.lang.reflect.Method.invoke(Method.java:511) 11-29 15:15:40.822: E/AndroidRuntime(6354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at dalvik.system.NativeStart.main(Native Method) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): Caused by: java.lang.NullPointerException 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.widget.Toast.(Toast.java:105) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.widget.Toast.makeText(Toast.java:261) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at com.ensil.vastusyscontrolunit.OutgoingReceiver.onReceive(OutgoingReceiver.java:25) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:771) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): ... 9 more

2 个答案:

答案 0 :(得分:2)

您必须使用onReceive()中代码如下的代码:

Intent intentcall = new Intent();
intentcall.setAction(Intent.ACTION_CALL);
intentcall.setData(Uri.parse("tel:" + phoneNumber)); 
intentcall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
context.startActivity(intentcall);  

你在代码中使用Toast了吗?以及如何调用它,因为Toast会使您的应用崩溃,对该行发表评论并重试或发布该信息以帮助您。

答案 1 :(得分:1)

你的

有些不对劲

com.ensil.vastusyscontrolunit.OutgoingReceiver.onReceive

第25行它表示空指针异常。