我正在尝试根据this问题将值传递给来自活动的广播接收器。
然而,这样做会导致我的应用程序崩溃。这是我的活动代码。
private void setAlarm(Calendar targetCal)
{
Intent in = new Intent("my.action.string");
in.putExtra("ALARM_NO", tempx);
sendBroadcast(in);
Intent alarmintent = new Intent(AddAlarm.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(AddAlarm.this, pen, alarmintent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), sender);
}
这里也是广播接收器代码:
public void onReceive(final Context context, Intent intent) {
int state;
String action = intent.getAction();
Log.i("Receiver", "Broadcast received: " + action);
if(action.equals("my.action.string"))
{
state = intent.getIntExtra("ALARM_NO",0);
}
if(state==1) {
//intent to call the activity which shows on ringing
Intent myIntent = new Intent(context, Time_Date.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
if(state==2)
{
//intent to call the activity which shows on ringing
Intent myIntent = new Intent(context, AlarmLaunch2.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
这就是我在logcat中获得的。
> 07-21 16:40:40.769 26721-26721/zyia.alarm.zyia.zyia E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: zyia.alarm.zyia.zyia, PID: 26721
java.lang.RuntimeException: Unable to start receiver zyia.alarm.zyia.zyia.AlarmReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2616)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at zyia.alarm.zyia.zyia.AlarmReceiver.onReceive(AlarmReceiver.java:29)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2609)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
那么,什么导致空指针异常?以及如何解决这个问题?任何帮助将不胜感激。
答案 0 :(得分:1)
你应该这样做:
private void setAlarm(Calendar targetCal)
{
Intent alarmintent = new Intent(AddAlarm.this, AlarmReceiver.class);
alarmintent.putExtra("ALARM_NO", tempx);
alarmintent.setAction("my.action.string");
PendingIntent sender = PendingIntent.getBroadcast(AddAlarm.this, pen, alarmintent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), sender);
}
alarmintent.setAction(" my.action.string&#34);
答案 1 :(得分:0)
您可以使用putExtra本身进行比较
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="more">click for more</div>
<div class="dropdown">Droppie down</div>
广播接收器中的getStringExtra(&#34; action&#34;)