简体   繁体   English

启动活动 android.os.TransactionTooLargeException 时出现异常:数据包大小

[英]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.

相关问题 android.os.TransactionTooLargeException:数据包大小NOUGAT错误 - android.os.TransactionTooLargeException: data parcel size NOUGAT ERROR android.os.TransactionTooLargeException:数据包大小565156字节 - android.os.TransactionTooLargeException: data parcel size 565156 bytes java.lang.RuntimeException:android.os.TransactionTooLargeException:在片段之间导航时数据包大小558780字节 - java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 558780 bytes when navigating between fragment 由于java.lang.RuntimeException而在图库中崩溃:android.os.TransactionTooLargeException:数据包大小539544字节 - Crash in gallery due to java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 539544 bytes 使用 Retrofit 调用获取 Web 服务请求时。 “android.os.TransactionTooLargeException:数据包大小 1575704 字节” - while calling get web service request Using Retrofit. “android.os.TransactionTooLargeException: data parcel size 1575704 bytes” 执行queryIntentActivities时android.os.TransactionTooLargeException - android.os.TransactionTooLargeException when executing queryIntentActivities 从 Fragment 移动到 Activity 导致 android.os.TransactionTooLargeException - Move from Fragment to Activity Causes android.os.TransactionTooLargeException android.os.TransactionTooLargeException的牛轧糖 - android.os.TransactionTooLargeException on Nougat 启动器中的android.os.transactiontoolargeexception - android.os.transactiontoolargeexception in Launcher 与其他应用共享图片时获取android.os.TransactionTooLargeException - Getting android.os.TransactionTooLargeException when sharing picture with others app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM