You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Intent获取ArrayList存入Fragment时抛出Cannot Be cast to java.util.ArrayList异常

Fixing "Cannot be cast to java.util.ArrayList" in Fragment when Retrieving Parcelable Data

Hey there! Let's break down why you're hitting this casting error when trying to pull your ArrayList from the Intent in your Fragment. This is a super common snag, and it almost always comes down to mismatched List types or small oversights in how you're passing/receiving the data.

Common Causes & Fixes

1. You're passing a non-java.util.ArrayList List instance

The most likely culprit is that the "ArrayList" you're putting into the Intent isn't actually a java.util.ArrayList. For example:

  • If you used Arrays.asList() to create your list, that returns a fixed-size ArrayList subclass (from java.util.Arrays, not the top-level ArrayList) which can't be cast to java.util.ArrayList.
  • Or you might be passing a different List implementation like LinkedList.

Fix: Explicitly wrap your data in a java.util.ArrayList when putting it into the Intent:

// In your Adapter, when creating the Intent
List<YourParcelableModel> sourceData = ...; // Could be any List
// Convert to a proper java.util.ArrayList
ArrayList<YourParcelableModel> parcelableList = new ArrayList<>(sourceData);

Intent intent = new Intent(getContext(), YourHostActivity.class);
intent.putParcelableArrayListExtra("YOUR_DATA_KEY", parcelableList);
startActivity(intent);

2. You're casting incorrectly in the Fragment

Even if you passed a proper ArrayList, sometimes the way you retrieve it can cause issues. The getParcelableArrayListExtra() method returns a List<T> (not guaranteed to be an ArrayList), so casting directly to ArrayList might fail if the system returns a different implementation.

Fix: Either:

  • Retrieve it as a List first, then convert to ArrayList:
// In your Fragment
List<YourParcelableModel> tempList = getActivity().getIntent().getParcelableArrayListExtra("YOUR_DATA_KEY");
if (tempList != null) {
    ArrayList<YourParcelableModel> yourDataList = new ArrayList<>(tempList);
    // Use yourDataList here
}
  • Or, if you're sure the data is an ArrayList, cast safely with a check:
Object data = getActivity().getIntent().getSerializableExtra("YOUR_DATA_KEY");
if (data instanceof ArrayList<?>) {
    ArrayList<YourParcelableModel> yourDataList = (ArrayList<YourParcelableModel>) data;
    // Proceed with your logic
}

3. Double-check your Parcelable implementation

While less likely, a broken Parcelable model can sometimes trigger unexpected casting errors. Make sure your YourParcelableModel class correctly implements Parcelable:

  • The CREATOR field is properly defined.
  • writeToParcel() writes all fields in the same order that createFromParcel() reads them.
  • You've overridden describeContents() (return 0 if you don't use special flags).

Quick Recap

The core issue is almost always that the List being passed isn't a true java.util.ArrayList. By explicitly creating an ArrayList when putting data into the Intent, or converting the retrieved List to an ArrayList in your Fragment, you should fix this error immediately.

内容的提问来源于stack exchange,提问作者kav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:36:26