具有对象ArrayList的类Parcelable

时间:2018-01-21 19:40:32

标签: java android object arraylist parcelable

我试图在包含Objects的ArrayList的类上使用Parcelable。

我在尝试编写列表时遇到错误。

班级 -

public class Library implements Parcelable {

ArrayList<Station> stations;
private String[] stationNames;
private String[] stationsDescription;
private int[] images;
private String[] streamLinks;

public Library(String[] stationNames, String[] stationsDescription, int[] images, String[] streamLinks) {
    //instantiationCounter++;
    this.stationNames = stationNames;
    this.stationsDescription = stationsDescription;
    this.images = images;
    this.streamLinks = streamLinks;

    this.stations = new ArrayList<>();

    for (int i = 0; i < stationNames.length; i++) {
        stations.add(new Station(stationNames[i], stationsDescription[i], streamLinks[i], images[i]));
    }
}

public Library() {
}

protected Library(Parcel in) {
    stationNames = in.createStringArray();
    stationsDescription = in.createStringArray();
    images = in.createIntArray();
    streamLinks = in.createStringArray();
    stationNames = in.createStringArray();
    stations = in.readArrayList(null);

}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeStringArray(stationNames);
    dest.writeStringArray(stationsDescription);
    dest.writeIntArray(images);
    dest.writeStringArray(streamLinks);
    dest.writeList(stations);
}

Logcat -

  

java.lang.RuntimeException:Parcel:无法编组值com.tsuryohananov.israeliradio.Station@8b5e126                                                                                       在android.os.Parcel.writeValue(Parcel.java:1711)                                                                                       在android.os.Parcel.writeList(Parcel.java:865)                                                                                       在com.tsuryohananov.israeliradio.Library.writeToParcel(Library.java:65)                                                                                       在android.os.Parcel.writeParcelable(Parcel.java:1730)                                                                                       在android.os.Parcel.writeValue(Parcel.java:1636)                                                                                       在android.os.Parcel.writeArrayMapInternal(Parcel.java:777)                                                                                       在android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1506)                                                                                       在android.os.Bundle.writeToParcel(Bundle.java:1181)                                                                                       在android.os.Parcel.writeBundle(Parcel.java:817)                                                                                       在android.content.Intent.writeToParcel(Intent.java:9480)                                                                                       在android.app.IActivityManager $ Stub $ Proxy.startService(IActivityManager.java:4877)                                                                                       在android.app.ContextImpl.startServiceCommon(ContextImpl.java:1491)                                                                                       在android.app.ContextImpl.startService(ContextImpl.java:1461)                                                                                       在android.content.ContextWrapper.startService(ContextWrapper.java:644)                                                                                       在com.tsuryohananov.israeliradio.Tab1Fragment $ 1.onItemClick(Tab1Fragment.java:95)                                                                                       在android.widget.AdapterView.performItemClick(AdapterView.java:318)                                                                                       在android.widget.AbsListView.performItemClick(AbsListView.java:1165)                                                                                       在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3134)                                                                                       在android.widget.AbsListView $ 3.run(AbsListView.java:4049)                                                                                       在android.os.Handler.handleCallback(Handler.java:789)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:98)                                                                                       在android.os.Looper.loop(Looper.java:164)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:6541)                                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                                       在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

更新: 正如Naveen Dew建议的那样,Station现在正在实施Parcelable。

站班 -

public class Station implements Parcelable {
    String name;
    String description;
    String streamLink;
    int img;

    public Station(String name, String description, String streamLink, int img) {
        this.name = name;
        this.description = description;
        this.streamLink = streamLink;
        this.img = img;
    }

    protected Station(Parcel in) {
        name = in.readString();
        description = in.readString();
        streamLink = in.readString();
        img = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(description);
        dest.writeString(streamLink);
        dest.writeInt(img);
    }

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

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

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

    String getStationName() {
        return name;
    }

    String getDescription() {
        return description;
    }

    String getStreamLink() {
        return streamLink;
    }

    int getStationImg() {
        return img;
    }

    void details() {
        System.out.println(name);
        System.out.println(description);
        System.out.println(streamLink);
        System.out.println(img);
        System.out.println("--------------");
    }
}

我将图书馆地块传递给服务部门时遇到了问题,而且是为了声音。

这就是我如何向服务发送额外的意图 -

                Intent serviceIntent = new Intent(getActivity(), BackgroundSoundService.class);
            serviceIntent.putExtra("Library", (Parcelable) mainLibrary);
            serviceIntent.putExtra("position", position);
            getContext().startService(serviceIntent);

这里我试图在BackGroundSoundService中获取库 -

    public int onStartCommand(Intent intent, int flags, int startId) {


    library = (Library) intent.getParcelableExtra("Library");
    position = intent.getIntExtra("position", 0);

}

无法启动服务!

Logcat -

  

java.lang.RuntimeException:无法启动服务com.tsuryohananov.israeliradio.BackgroundSoundService@823fbd with Intent {cmp = com.tsuryohananov.israeliradio / .BackgroundSoundService(has extras)}:java.lang.RuntimeException:Parcel android。 os.Parcel@5f24b14:在偏移量10964处解组未知类型代码7274617                                                                                       在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3556)                                                                                       在android.app.ActivityThread.-wrap20(未知来源:0)                                                                                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1698)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:105)                                                                                       在android.os.Looper.loop(Looper.java:164)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:6541)                                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                                       在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)                                                                                    引起:java.lang.RuntimeException:Parcel android.os.Parcel@5f24b14:在偏移10964处解组未知类型代码7274617                                                                                       在android.os.Parcel.readValue(Parcel.java:2754)                                                                                       在android.os.Parcel.readListInternal(Parcel.java:3103)                                                                                       在android.os.Parcel.readArrayList(Parcel.java:2305)                                                                                       在com.tsuryohananov.israeliradio.Library。(Library.java:64)                                                                                       在com.tsuryohananov.israeliradio.Library $ 1.createFromParcel(Library.java:103)                                                                                       在com.tsuryohananov.israeliradio.Library $ 1.createFromParcel(Library.java:100)                                                                                       在android.os.Parcel.readParcelable(Parcel.java:2781)                                                                                       在android.os.Parcel.readValue(Parcel.java:2675)                                                                                       在android.os.Parcel.readArrayMapInternal(Parcel.java:3042)                                                                                       在android.os.BaseBundle.unparcel(BaseBundle.java:257)                                                                                       在android.os.Bundle.getParcelable(Bundle.java:888)                                                                                       在com.tsuryohananov.israeliradio.BackgroundSoundService.onStartCommand(BackgroundSoundService.java:62)                                                                                       在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3539)

更新 -

清单 -

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!--
        <activity
        android:name=".NotificationView"
        android:configChanges="orientation|screenSize"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        </activity>
    -->


    <service
        android:name=".BackgroundSoundService"
        android:label="My Service">
    </service>

    <activity android:name=".PlayActivity"></activity>
</application>

1 个答案:

答案 0 :(得分:0)

你的内部课程也应该是Parcelable

检查Station是否正在实施Parcelable

<强>更新

Creator

中缺少

Library

将其添加到您的图书馆类

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

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

也请发布您的Manifest文件