我想获得拨打我的申请的电话号码。但是当我收到一个蒙面号码时,我的应用程序崩溃了。我使用onCallStateChanged()。我简化了我的测试代码。它仍然坠毁。这是代码(它们在一个活动中):
public void onCreate() {
super.onCreate();
TelephonyManager manager = (TelephonyManager) getSystemService(Service.TELEPHONY_SERVICE);
manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
private PhoneStateListener listener = new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber){
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
if(incomingNumber.isEmpty()){
Log.i("PhoneStateListener incomingNumber", "void");
}else{
Log.i("PhoneStateListener incomingNumber", "not void");
}
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
default:
break;
}
}
};
我得到的错误:
01-29 12:10:37.479: W/dalvikvm(7141): threadid=1: thread exiting with uncaught exception (group=0x41ddc700)
01-29 12:10:37.484: E/AndroidRuntime(7141): FATAL EXCEPTION: main
01-29 12:10:37.484: E/AndroidRuntime(7141): java.lang.NullPointerException
01-29 12:10:37.484: E/AndroidRuntime(7141): at com.aif.app.MonitorCallingService$1.onCallStateChanged(MonitorCallingService.java:77)
01-29 12:10:37.484: E/AndroidRuntime(7141): at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:454)
01-29 12:10:37.484: E/AndroidRuntime(7141): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 12:10:37.484: E/AndroidRuntime(7141): at android.os.Looper.loop(Looper.java:176)
01-29 12:10:37.484: E/AndroidRuntime(7141): at android.app.ActivityThread.main(ActivityThread.java:5419)
01-29 12:10:37.484: E/AndroidRuntime(7141): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 12:10:37.484: E/AndroidRuntime(7141): at java.lang.reflect.Method.invoke(Method.java:525)
01-29 12:10:37.484: E/AndroidRuntime(7141): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
01-29 12:10:37.484: E/AndroidRuntime(7141): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
01-29 12:10:37.484: E/AndroidRuntime(7141): at dalvik.system.NativeStart.main(Native Method)
onCallStateChanged()函数中是否存在带有掩码数的内部错误?
任何人都可以帮助我吗?非常感谢。
答案 0 :(得分:1)
我的猜测:参数incomingNumber
为空。所以改变if语句:
if(incomingNumber != null || incomingNumber.isEmpty())
答案 1 :(得分:1)
当onCallStateChanged()收到未知数字时,参数incomingNumber为null,而不是字符串对象。因此,对象字符串函数的任何调用都将调用错误。因为物体不存在。所以if中的条件应该只是
if(incomingNumber!=null) or if(incomingNumber==null)
这是一个空字符串和一个空字符串的问题。
答案 2 :(得分:0)
在onCallStateChanged中使用try catch块。