开始活动android.os.TransactionTooLargeException时发生异常:数据包裹大小 [英] Exception when starting activity android.os.TransactionTooLargeException: data parcel size

查看:705
本文介绍了开始活动android.os.TransactionTooLargeException时发生异常:数据包裹大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用大量额外数据创建意图

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;
   }

然后运行活动: 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?

推荐答案

您正在通过Intent将整个List<PhotoItem>传递给您的GalleryViewActivity.因此,您的List<PhotoItem>列表可能包含许多数据.因此有时系统无法一次处理大量数据.

You are passing whole List<PhotoItem> to your GalleryViewActivity with Intent. So it might possible that your list of List<PhotoItem> can have many data. So sometime system can not handle much data to transfer at a time.

请避免使用Intent传递大量数据.

Please avoid to pass large amount of data with Intent.

您可以使用SharedPreferences存储阵列列表,并在其他活动中检索该列表.

You can use SharedPreferences to store your array list and retrieve the same on other activity.

使用以下方法初始化您的SharedPreferences:

Initialize your SharedPreferences using:

SharedPreferences prefrence =  PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = prefrence.edit();

您可以使用这种方式将列表存储在Preference变量中

You can use this way to store list in Preference variable

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;
}

现在在您的GalleryViewActivity.java文件中

Now in your GalleryViewActivity.java file

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());

您将在listGallery变量中拥有列表.您可以像现在使用的一样检索索引.

You will have your list in listGallery variable. You can retrieve your index as the same way you are using right now.

这篇关于开始活动android.os.TransactionTooLargeException时发生异常:数据包裹大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆