将ParcebleArrayList放入Bundle以调用新intent

时间:2015-03-30 20:55:40

标签: android android-intent

我试图将这个类放在一个数组中并将其发送到一个包中 bundle.putParcelableArrayList,关于它的任何提示?

以下是我实施Parcelable

的课程
package neocom.dealerbook.model;


import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by viana.wesley on 30/03/15.
 */
public class LojaHeader implements Parcelable {

    private int id;
    private int idOriginal;
    private int logo;
    private String nome;
    private String endereco;

    public LojaHeader() {
    }

    public LojaHeader(Parcel source){
        id = source.readInt();
        idOriginal = source.readInt();
        logo = source.readInt();
        nome = source.readString();
        endereco = source.readString();
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(id);
        parcel.writeInt(idOriginal);
        parcel.writeInt(logo);
        parcel.writeString(nome);
        parcel.writeString(endereco);
    }

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


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getIdOriginal() {
        return idOriginal;
    }

    public void setIdOriginal(int idOriginal) {
        this.idOriginal = idOriginal;
    }

    public int getLogo() {
        return logo;
    }

    public void setLogo(int logo) {
        this.logo = logo;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    public static final Parcelable.Creator<LojaHeader> CREATOR = new Parcelable.Creator<LojaHeader>(
        @Override
        public LojaHeader createFromParcel(Parcel parcel) {
            return new LojaHeader(parcel);
        }

        @Override
        public LojaHeader[] newArray(int i) {
            return new LojaHeader[i];
        }
    };

}

以下是我如何调用此意图。

public void callPointListActivity(View view) {
        List<Marker> markers = mMap.getMarkers();
        ArrayList<LojaHeader> lojaHeaders = new ArrayList<>();
        Log.d(TAG, markers.toString());

        for (Marker m : markers){
            Loja loja = m.getData();
            lojaHeaders.add(loja.getLojaHeader());
        }


        Intent intent = new Intent(getActivity(), PointListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("LojaHeader", lojaHeaders);
        startActivity(intent, bundle);

    }

我收到此错误

Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: neocom.dealerbook.model.LojaHeader
            at android.os.Parcel.readException(Parcel.java:1474)
            at android.os.Parcel.readException(Parcel.java:1426)
            at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2115)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1536)
            at android.app.Activity.startActivityFromFragment(Activity.java:3957)
            at android.app.Fragment.startActivity(Fragment.java:1050)
            at neocom.dealerbook.MapFragment.callPointListActivity(MapFragment.java:701)
            at neocom.dealerbook.MapActivity.callPointListActivity(MapActivity.java:95)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3825)
            at android.view.View.performClick(View.java:4445)
            at android.view.View$PerformClick.run(View.java:18446)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您错误地将数据添加到意图中。你打电话给startActivity(Intent, Bundle)。在这种情况下,BundlestartActivity()电话的选项。你想这样做,

Intent intent = new Intent(getActivity(), PointListActivity.class);
intent.putParcelableArrayListExtra("LojaHeader", lojaHeaders);
startActivity(intent); 

ClassNotFoundException是因为框架试图取消选项Bundle,但找不到你的类,因为它不在Android框架中。