我扩展了BroadcastReceiver:
public class MyBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (null == bundle)
return;
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
String currentTimeString = DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());
int a = 2;
if (currentTimeString.length() == 5) {
a = 3;
}
String minutes = currentTimeString.substring(a);
if (phoneNumber.equals(minutes)) {
//some code
}
}
}
我的一位用户使用ACRA跟踪崩溃报告(所以,我不能重复此错误):
"java.lang.RuntimeException: Unable to start receiver myapp.service.MyBroadcast: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2144)
at android.app.ActivityThread.access$2400(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1114)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4389)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at myapp.service.MyBroadcast.onReceive(MyBroadcast.java:31)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2103)
... 10 more
java.lang.NullPointerException
at myapp.service.MyBroadcast.onReceive(MyBroadcast.java:31)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2103)
at android.app.ActivityThread.access$2400(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1114)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4389)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)"
因此,第31行中存在NullPointerException:
if (phoneNumber.equals(minutes)) {
据我所知,字符串phoneNumber
或minutes
之一为空。因为我的理解intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)
在我的情况下不能为空(或者我错了?)。我假设currentTimeString
长度为4或5个符号(例如,它返回1:45或20:54)。也许原因是遇到此错误的用户有locale en_PK(巴基斯坦 - 我认为)和DateFormat.getTimeInstance
以某种奇怪的格式返回时间,但我认为应该有nothing strange ... < / p>
导致此NullPointerException
的原因是什么?
答案 0 :(得分:0)
当然phoneNumber
为空,因为String.equals(null)
只会返回false。根据{{3}},ACTION_NEW_OUTGOING_CALL
的接收者可以修改该数字,包括如果不应该被调用则将其设置为空(例如,父母控制应用程序)。所以我认为你的用户就是这种情况,即你的接收者得到的号码无效。
BTW正如@Puru所说,不推荐你提取分钟的方法。