Java 拨电话时发生空指针异常
舱单:Java 拨电话时发生空指针异常,java,android,nullpointerexception,Java,Android,Nullpointerexception,舱单: <receiver android:name=".CallReceiver"> <intent-filter android:priority="100" > <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver> 如果我键入,空
<receiver android:name=".CallReceiver">
<intent-filter android:priority="100" >
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
如果我键入,空指针错误将消失
String incommingNumber = "123";
有什么想法吗?问题在于以下几行:
String incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(incommingNumber.length()!=0)
b、 getString返回null和null。length()抛出错误。你需要加一张这样的支票
if (incomingNumber == null) {
// do something
}
以下是getString的工作原理:
Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.
Parameters
key a String, or null
defaultValue Value to return if key does not exist
Returns
a String value, or null
->
->使用Bundle b=getIntent.getExtras();你需要检查一下这个箱子吗?如上所述。如果数字是隐藏的,则函数可能返回null。
Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.
Parameters
key a String, or null
defaultValue Value to return if key does not exist
Returns
a String value, or null