Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 拨电话时发生空指针异常_Java_Android_Nullpointerexception - Fatal编程技术网

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