问题
我不知道为什么logcat会给我一个NPE,因为意图不是null并且在onReceive()中有额外的 所以,请帮我找出并指导我的事情。
注意
Logcat(证明它有额外的内容)
10-19 06:10:32.587 31593-31593/com.bis.prototype.airdroid E/BroadcastReceiver_CalibrateMode﹕ Intent { flg=0x14 cmp=com.bis.prototype.airdroid/.BroadcastReceiver_CalibrateMode (has extras) }
BroadcastReceiver_CalibrateMode.class
public class BroadcastReceiver_CalibrateMode extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("BroadcastReceiver_CalibrateMode", String.valueOf(intent));
String slot = intent.getExtras().getString("NODE_SLOT");
Log.e("BroadcastReceiver_CalibrateMode", slot); // NPE ON THIS LINE
Intent calibrateIntent = new Intent(context, Service_MQTTPush.class);
calibrateIntent.putExtra("CALIBRATE_MODE",true);
calibrateIntent.putExtra("NODE_SLOT", slot);
context.startService(calibrateIntent);
}
}
logcat的
java.lang.RuntimeException: Unable to start receiver com.bis.prototype.airdroid.BroadcastReceiver_CalibrateMode: java.lang.NullPointerException: println needs a message
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2476)
at android.app.ActivityThread.access$1700(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1322)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.e(Log.java:232)
at com.bis.prototype.airdroid.BroadcastReceiver_CalibrateMode.onReceive(BroadcastReceiver_CalibrateMode.java:17)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2469)
at android.app.ActivityThread.access$1700(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1322)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
我注册广播的代码
public static void registerAlarm_calibrateMode(Context context){
for (int i = 0; i < NODE_SLOT.length; i++) {
Log.e("registerAlarm_calibrateMode", NODE_SLOT[i]+" :: "+getNodeSerial(context,NODE_SLOT[i]));
if (!getNodeSerial(context,NODE_SLOT[i]).equals("null")) {
Intent intent = new Intent(context, BroadcastReceiver_CalibrateMode.class);
Log.e("registerAlarm_calibrateMode", "putExtra-->"+NODE_SLOT[i]);
intent.putExtra("NODE_SLOT", NODE_SLOT[i]);
PendingIntent pi = PendingIntent.getBroadcast(
context,
GET_NODE_BROADCAST_ID_FOR_CALIBRATE_MODE[getNodeID(NODE_SLOT[i])],
intent,
Intent.FILL_IN_DATA
);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar now = Calendar.getInstance();
now.add(Calendar.MINUTE,1);
if (android.os.Build.VERSION.SDK_INT < 19) {
alarm.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), pi);
} else {
alarm.setExact(AlarmManager.RTC_WAKEUP,now.getTimeInMillis(),pi);
}
}
}
}
XML MANIFEST
<receiver android:name=".BroadcastReceiver_SleepMode"/>
<receiver android:name=".BroadcastReceiver_Location"/>
<receiver android:name=".BroadcastReceiver_CalibrateMode"/>
<receiver android:name=".BroadcastReceiver_TempLog"/>
答案 0 :(得分:2)
替换流动的代码 BroadcastReceiver_CalibrateMode.java
public class BroadcastReceiver_CalibrateMode extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("BroadcastReceiver_CalibrateMode", String.valueOf(intent));
String slot = intent.getExtras().getString("NODE_SLOT");
Log.e("BroadcastReceiver_CalibrateMode", slot); // NPE ON THIS LINE
Intent calibrateIntent = new Intent(context, Service_MQTTPush.class);
calibrateIntent.putExtra("CALIBRATE_MODE",true);
calibrateIntent.putExtra("NODE_SLOT", slot);
calibrateIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // SPCIAL CODE
context.startService(calibrateIntent);
}
}
调用startActivity时,新活动将与调用者位于同一屏幕堆栈(上下文)中。有时,调用者可能没有活动上下文,例如,从 Broadcastreceive.onReceive()方法启动活动,或从服务启动活动。在这些情况下,调用startActivity将导致“从Activity活动上下文调用startActivity()”的异常需要 FLAG_ACTIVITY_NEW_TASK “,因为新活动没有生成的上下文。
要解决此问题,请按如下方式将FLAG_ACTIVITY_NEW_TASK设置为意图,然后将为新活动创建新的上下文。
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);