广播接收器中的Toast

时间:2014-03-11 08:12:52

标签: android broadcastreceiver

我正在尝试一个简单的代码,以便在收到呼叫时将呼叫者号码显示为吐司。

我收到以下错误:

 03-11 13:34:41.143: E/AndroidRuntime(27141): java.lang.NullPointerException
 03-11 13:34:41.143: E/AndroidRuntime(27141):   at android.widget.Toast.<init>(Toast.java:93)
 03-11 13:34:41.143: E/AndroidRuntime(27141):   at android.widget.Toast.makeText(Toast.java:241)
 03-11 13:34:41.143: E/AndroidRuntime(27141):   at com.demotest.ringer.PhoneListenerBroad$MyPhoneStateListener.onCallStateChanged(PhoneListenerBroad.java:47)
 03-11 13:34:41.143: E/AndroidRuntime(27141):   at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:369)
 03-11 13:34:41.143: E/AndroidRuntime(27141):   at android.os.Handler.dispatchMessage(Handler.java:102)
 03-11 13:34:41.143: E/AndroidRuntime(27141):   at android.os.Looper.loop(Looper.java:136)
 03-11 13:34:41.143: E/AndroidRuntime(27141):   at android.app.ActivityThread.main(ActivityThread.java:5017)
 03-11 13:34:41.143: E/AndroidRuntime(27141):   at java.lang.reflect.Method.invokeNative(Native Method)
 03-11 13:34:41.143: E/AndroidRuntime(27141):   at java.lang.reflect.Method.invoke(Method.java:515)
 03-11 13:34:41.143: E/AndroidRuntime(27141):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 03-11 13:34:41.143: E/AndroidRuntime(27141):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 03-11 13:34:41.143: E/AndroidRuntime(27141):   at dalvik.system.NativeStart.main(Native Method)

以下是我正在尝试的代码:

 public class PhoneListenerBroad extends BroadcastReceiver  
  { 
    @Override
public void onReceive(Context context, Intent intent) 
{
    try 
    {
        // TELEPHONY MANAGER class object to register one listner
        TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        //Create Listner
        MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

        // Register listener for LISTEN_CALL_STATE
        tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    } 
    catch (Exception e) 
    {
        Log.e("Phone Receive Error", " " + e);
    }

}

private class MyPhoneStateListener extends PhoneStateListener 
{
    private Context context;

    public void onCallStateChanged(int state, String incomingNumber) 
    {

        Log.e("MyPhoneListener",state+"   incoming no:"+incomingNumber);

        if (state == 1) 
        {
            String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    }
}
}

Manifest需要权限和声明..我不知道我哪里出错了?

4 个答案:

答案 0 :(得分:2)

您的上下文未初始化。初始化上下文或代替上下文尝试使用“getApplicationContext()”“getBaseContext()”

Toast.makeText(getApplicationContext(),msg , Toast.LENGTH_SHORT).show();

或者

Toast.makeText(getBaseContext(),msg , Toast.LENGTH_SHORT).show();

希望这能解决您的问题。

答案 1 :(得分:2)

public class PhoneListenerBroad extends BroadcastReceiver{
Context c;

@Override
public void onReceive(Context context, Intent intent) {
    c = context;
    try {
        // TELEPHONY MANAGER class object to register one listner
        TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        // Create Listner
        MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

        // Register listener for LISTEN_CALL_STATE
        tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    } catch (Exception e) {
        Log.e("Phone Receive Error", " " + e);
    }

}

private class MyPhoneStateListener extends PhoneStateListener
{
    private Context context;

    public void onCallStateChanged(int state, String incomingNumber) {

        Log.e("MyPhoneListener", state + "   incoming no:" + incomingNumber);

        if (state == 1) {
            String msg = "New Phone Call Event. Incomming Number : " + incomingNumber;
            Toast.makeText(c, msg, Toast.LENGTH_SHORT).show();
        }
    }
}}

答案 2 :(得分:1)

您的班级context中的成员字段MyPhoneStateListener永远不会设置。用当前上下文初始化它。

[...]
//Create Listener
MyPhoneStateListener PhoneListener = new MyPhoneStateListener(context);
[...]



private class MyPhoneStateListener extends PhoneStateListener  {

    private Context mContext;

    private void MyPhoneStateListener() {
    }

    public void MyPhoneStateListener(Context context) {
        mContext = context;
    }

    public void onCallStateChanged(int state, String incomingNumber)  {

        Log.e("MyPhoneListener",state+"   incoming no:"+incomingNumber);

        if (state == 1) {
            String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
            Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
        }
    }
}

答案 3 :(得分:1)

MyPhoneStateListener

创建构造函数
Context mContext;
public void MyPhoneStateListener(Context context) {
    mContext = context;
}

现在在你的Toast中使用这个mContext变量。

String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();