[英]Exception when starting activity android.os.TransactionTooLargeException: data parcel size
Creating intent with a large amount of data in extras使用额外的大量数据创建意图
public static Intent createIntent(Context context, List<PhotoItem> gallery, int indexOf) {
Intent intent = new Intent(context, GalleryViewActivity.class);
intent.putExtra(EXTRA_PHOTO_INDEX, indexOf);
intent.putExtra(EXTRA_PHOTO_OBJECT, new Gson().toJson(gallery));
return intent;
}
Then running the activity: startActivity(createIntent(...
然后运行活动:
startActivity(createIntent(...
crashes application with error:应用程序崩溃并出现错误:
Exception when starting activity android.os.TransactionTooLargeException: data parcel size...
How to avoid such errors when data is too large in the list?当列表中的数据太大时如何避免此类错误?
You are passing whole List<PhotoItem>
to your GalleryViewActivity
with Intent
.您正在通过
Intent
将整个List<PhotoItem>
传递给您的GalleryViewActivity
。 So it might possible that your list of List<PhotoItem>
can have many data.因此,您的
List<PhotoItem>
可能有很多数据。 So sometime system can not handle much data to transfer at a time.因此,有时系统无法一次处理大量要传输的数据。
Please avoid to pass large amount of data with Intent.请避免使用 Intent 传递大量数据。
You can use SharedPreferences
to store your array list and retrieve the same on other activity.您可以使用
SharedPreferences
来存储您的数组列表并在其他活动中检索相同的列表。
Initialize your SharedPreferences using:
使用以下方法初始化您的 SharedPreferences:
SharedPreferences prefrence = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = prefrence.edit();
You can use this way to store list in Preference variable
您可以使用这种方式将列表存储在 Preference 变量中
public static Intent createIntent(Context context, List<PhotoItem> gallery, int indexOf) {
Intent intent = new Intent(context, GalleryViewActivity.class);
intent.putExtra(EXTRA_PHOTO_INDEX, indexOf);
editor.putString("GallaryData", new Gson().toJson(gallery));
editor.commit();
return intent;
}
Now in your GalleryViewActivity.java file
现在在你的 GalleryViewActivity.java 文件中
SharedPreferences prefrence = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = prefrence.edit();
String galleryData = prefrence.getString("GallaryData", "");
List<PhotoItem> listGallery = new Gson().fromJson(galleryData, new TypeToken<List<PhotoItem>>() {}.getType());
You will have your list in listGallery variable.您将在 listGallery 变量中拥有您的列表。 You can retrieve your index as the same way you are using right now.
您可以像现在使用的一样检索索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.