我正在做一个在后台播放音乐以供通话的应用程序,我的应用程序有一个主要活动,一个广播监听器和一个服务。
广播监听器的代码是
public class CallBrdcstListener extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
PhoneStateDisplay phoneListener=new PhoneStateDisplay();
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
}
}
电话状态监听器是
public class PhoneStateDisplay extends PhoneStateListener{
private Context context;
public void onCallStateChanged(int state,String incomingNumber){
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK");
System.out.println("Calling Intent");
Intent musicIntent = new Intent(context,MusicService.class);
context.startService(musicIntent);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
break;
}
}
}
它在意图创建部分显示错误......谁能告诉我错误是什么
logcat 输出是
01-18 15:45:57.250: I/System.out(2822): Calling Intent
01-18 15:46:30.453: E/AndroidRuntime(2857): FATAL EXCEPTION: main
01-18 15:46:30.453: E/AndroidRuntime(2857): java.lang.NullPointerException
01-18 15:46:30.453: E/AndroidRuntime(2857): at android.content.ComponentName.<init>(ComponentName.java:75)
01-18 15:46:30.453: E/AndroidRuntime(2857): at android.content.Intent.<init>(Intent.java:2823)
01-18 15:46:30.453: E/AndroidRuntime(2857): at com.example.backgroundmusicplay.PhoneStateDisplay.onCallStateChanged(PhoneStateDisplay.java:22)
01-18 15:46:30.453: E/AndroidRuntime(2857): at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:319)
01-18 15:46:30.453: E/AndroidRuntime(2857): at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 15:46:30.453: E/AndroidRuntime(2857): at android.os.Looper.loop(Looper.java:130)
01-18 15:46:30.453: E/AndroidRuntime(2857): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-18 15:46:30.453: E/AndroidRuntime(2857): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 15:46:30.453: E/AndroidRuntime(2857): at java.lang.reflect.Method.invoke(Method.java:507)
01-18 15:46:30.453: E/AndroidRuntime(2857): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-18 15:46:30.453: E/AndroidRuntime(2857): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-18 15:46:30.453: E/AndroidRuntime(2857): at dalvik.system.NativeStart.main(Native Method)