Object return 当 BroadcastReceiver 上的 putExtra 和 getSerializable 时为 null
Object return null when putExtra and getSerializable on BroadcastReceiver
我只想将标题弹出到通知中,但这是 return 空 object。我需要帮助,我的英语很糟糕希望大家同情:D.
错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.a.Work.getTitle()' on a null object reference at com.example.a.AlertBrr.onReceive(AlertBrr.java:24)
CreateActivity.class
Work work = new Work();
work.setTitle(ctitle.getText().toString());
work.setDate(edittext.getText().toString());
work.setContent(ccontent.getText().toString());
Database database = new Database(CreateActivity.this);
database.add(work);
//
Intent intent = new Intent(CreateActivity.this, AlertBrr.class);
intent.putExtra("work", work);
PendingIntent pendingIntent = PendingIntent.getBroadcast(CreateActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
AlertBrr.class:
public class AlertBrr extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Work work = (Work) intent.getSerializableExtra("work");
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "duong")
.setContentTitle(work.getTitle())
.setContentText(work.getContent())
.setContentInfo(work.getDate())
.setSmallIcon(R.drawable.done);
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
managerCompat.notify(100, builder.build());
}
}
Ha Minh,我看过你的项目。感谢分享,帮了大忙!
要解决此问题,您必须将 Work
对象包装到 Bundle
中,然后将 Bundle
传递到 Intnet
中。 CreateActivity#onOptionsItemSelected
中的代码应如下所示:
Intent intent = new Intent(CreateActivity.this, AlertBrr.class);
Bundle bundle = new Bundle();
bundle.putSerializable("work", work);
intent.putExtra("work_bundle", bundle);
在AlertBrr#onReceive
中:
Bundle workBundle = intent.getParcelableExtra("work_bundle");
Work work = (Work) workBundle.getSerializable("work");
注意:我得到了 Whosebug 答案的想法。
我只想将标题弹出到通知中,但这是 return 空 object。我需要帮助,我的英语很糟糕希望大家同情:D.
错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.a.Work.getTitle()' on a null object reference at com.example.a.AlertBrr.onReceive(AlertBrr.java:24)
CreateActivity.class
Work work = new Work();
work.setTitle(ctitle.getText().toString());
work.setDate(edittext.getText().toString());
work.setContent(ccontent.getText().toString());
Database database = new Database(CreateActivity.this);
database.add(work);
//
Intent intent = new Intent(CreateActivity.this, AlertBrr.class);
intent.putExtra("work", work);
PendingIntent pendingIntent = PendingIntent.getBroadcast(CreateActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
AlertBrr.class:
public class AlertBrr extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Work work = (Work) intent.getSerializableExtra("work");
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "duong")
.setContentTitle(work.getTitle())
.setContentText(work.getContent())
.setContentInfo(work.getDate())
.setSmallIcon(R.drawable.done);
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
managerCompat.notify(100, builder.build());
}
}
Ha Minh,我看过你的项目。感谢分享,帮了大忙!
要解决此问题,您必须将 Work
对象包装到 Bundle
中,然后将 Bundle
传递到 Intnet
中。 CreateActivity#onOptionsItemSelected
中的代码应如下所示:
Intent intent = new Intent(CreateActivity.this, AlertBrr.class);
Bundle bundle = new Bundle();
bundle.putSerializable("work", work);
intent.putExtra("work_bundle", bundle);
在AlertBrr#onReceive
中:
Bundle workBundle = intent.getParcelableExtra("work_bundle");
Work work = (Work) workBundle.getSerializable("work");
注意:我得到了