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

Activity间传递Location嵌套列表遇空值问题求助

Fixing Null ArrayList<List> When Passing Between Activities

Let's break down what's going on here and fix that null issue you're seeing:

The Core Problem

Android's Location class implements Parcelable, not Serializable. While your broadcast approach worked (likely because it was passing within the same process, where Serializable might just pass a reference instead of full serialization), Activity-to-Activity serialization triggers proper object serialization—and since Location isn't Serializable, the process fails silently, leaving you with a null list.

Additionally, even if Location were Serializable, nested generic lists like ArrayList<List<Location>> suffer from type erasure during deserialization, making it hard for the system to reconstruct the exact type correctly.

Since Location is already Parcelable, we can leverage Android's optimized Parcelable system to pass your nested list reliably. Here's how:

Step 1: Convert to Parcelable-Compatible List

First, convert your ArrayList<List<Location>> to ArrayList<ArrayList<Location>> (since ArrayList implements Parcelable):

// In your sending Activity's onEnd method
ArrayList<ArrayList<Location>> parcelableLocations = new ArrayList<>();
for (List<Location> subList : locationList) {
    parcelableLocations.add(new ArrayList<>(subList));
}
end.putParcelableArrayListExtra("locations", parcelableLocations);

Step 2: Retrieve in the Receiving Activity

// In onCreate of your receiving Activity
locationList = getIntent().getParcelableArrayListExtra("locations");
// No need for casting—getParcelableArrayListExtra returns the correct type
if (locationList != null) {
    // Your existing logic here
}

Solution 2: Custom Parcelable Wrapper (For Strict Nested List Types)

If you need to keep the exact ArrayList<List<Location>> type, create a custom Parcelable wrapper to handle the nested list:

Create the Wrapper Class

import android.location.Location;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;

public class LocationListWrapper implements Parcelable {
    private ArrayList<List<Location>> locationLists;

    public LocationListWrapper(ArrayList<List<Location>> locationLists) {
        this.locationLists = locationLists;
    }

    protected LocationListWrapper(Parcel in) {
        locationLists = new ArrayList<>();
        int listCount = in.readInt();
        for (int i = 0; i < listCount; i++) {
            ArrayList<Location> subList = in.createTypedArrayList(Location.CREATOR);
            locationLists.add(subList);
        }
    }

    public static final Creator<LocationListWrapper> CREATOR = new Creator<LocationListWrapper>() {
        @Override
        public LocationListWrapper createFromParcel(Parcel in) {
            return new LocationListWrapper(in);
        }

        @Override
        public LocationListWrapper[] newArray(int size) {
            return new LocationListWrapper[size];
        }
    };

    public ArrayList<List<Location>> getLocationLists() {
        return locationLists;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(locationLists.size());
        for (List<Location> subList : locationLists) {
            dest.writeTypedList(subList);
        }
    }
}

Send the Wrapper

// In your sending Activity
LocationListWrapper wrapper = new LocationListWrapper(locationList);
end.putExtra("locations", wrapper);

Retrieve the Wrapper

// In your receiving Activity
LocationListWrapper wrapper = getIntent().getParcelableExtra("locations");
if (wrapper != null) {
    locationList = wrapper.getLocationLists();
}

Why Your Broadcast Worked

When sending via broadcast within the same app process, Android often skips full serialization and just passes object references. This is why your Serializable approach worked there—but it's not reliable for cross-activity (or cross-process) transfers, where full serialization is required.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:58:14