脚本:
我有一个闹钟计划在指定的时间运行。每次执行时,我的广播接收器都会触发。
在BroadcastReceiver中,我做了各种检查,最后得到一个Notify类的ArrayList
我在状态栏上显示通知
当用户点击通知时,我会显示一个活动。我需要在活动中使用arraylist在视图上显示它。
下面是示例代码:
public class ReceiverAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ArrayList<Notify> notifications = new ArrayList<Notify>();
//do the checks, for exemplification I add these values
notifications.add(new Notify("id1","This is very important"));
notifications.add(new Notify("id2","This is not so important"));
notifications.add(new Notify("id3","This is way too mimportant"));
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//init some values from notificationManager
Intent intentNotif = new Intent(context, NotificationViewer.class);
intentNotif.putParcelableArrayListExtra("list", notifications);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotif, 0);
Notification notification = new Notification(icon, text, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
和
public class NotificationViewer extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_viewer);
ArrayList<Notify> testArrayList = null;
Bundle b = getIntent().getExtras();
if (b != null) {
testArrayList = b.getParcelableArrayList("list");
}
}
和
public class Notify implements Parcelable {
public Notify(Parcel in) {
readFromParcel(in);
}
@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Notify createFromParcel(Parcel in) {
return new Notify(in);
}
public Notify[] newArray(int size) {
return new Notify[size];
}
};
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(text);
}
private void readFromParcel(Parcel in) {
id = in.readString();
text = in.readString();
}
public Notify(String id, String text) {
super();
this.id = id;
this.text = text;
}
/** The id. */
public String id;
/** Notification text to be displayed. */
public String text;
@Override
public int describeContents() {
return 0;
}
}
在notificationactivity的testarraylist=b.getParcelableArraylist(“list”)上,我得到以下错误:
E/AndroidRuntime(14319): java.lang.RuntimeException: Unable to start activity
componentinfo{notificationviewer}:java.lang.runtimeexception:包
android.os.parcel@4050f960:在
偏移量124
如你所见,从《圣经》中我说我需要把我的东西包装起来。也许我在里面做错了什么,但是…我不知道怎么修。我做错什么了?
最佳答案
在NotificationViewer
活动获取值中,如下所示:
ArrayList<Notify> testArrayList = getIntent().getParcelableArrayListExtra("list");
您正在使用
putParcelableArrayListExtra()
放置值,因此需要使用getParcelableArrayListExtra()
而不是getParcelableArrayList()
。