简体   繁体   中英

Can't pass ArrayList<Object> through an intent after implementing Parcelable

After looking online and seeing that Parcelable is the way to go for passing an ArrayList of objects through an intent, I implemented it in nearly every single way possible and still cannot successfully pass it from one activity to another.

The class of the object I'm trying to pass through:

public class Occurrence implements Comparable<Occurrence>, Parcelable {
int apps;
String word;

public Occurrence(String word, int apps){
    this.apps = apps;
    this.word = word;
}

public Occurrence(Parcel source){
    apps = source.readInt();
    word = source.readString();
}


public int describeContents(){
    return this.hashCode();
}

public void writeToParcel(Parcel dest, int flags){
    dest.writeInt(apps);
    dest.writeString(word);
}

public static final Parcelable.Creator<Occurrence> CREATOR = new Parcelable.Creator(){
    public Occurrence[] newArray(int size){
        return new Occurrence[size];
    }
    public Occurrence createFromParcel(Parcel in){
        return new Occurrence(in);
    }
};

public int compareTo(Occurrence x){
    return(x.apps-this.apps);
}
}

Putting it in the intent:

 Intent resultIntent = new Intent(pickActivity.this, StatActivity.class);
            resultIntent.putExtra("words", words);
            resultIntent.putParcelableArrayListExtra("sortedWordList", sortedWordList); //an arraylist<occurrence>
            resultIntent.putParcelableArrayListExtra("ppl", sortedPplList); // an arraylist<occurrence

(trying) to receive it in my next activity:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.stats);

    HashMap<String, Integer> words =
            (HashMap<String, Integer>) getIntent().getSerializableExtra("words");


    ArrayList<Occurrence> ppl = getIntent().getParcelableArrayListExtra("ppl");


    ArrayList<Occurrence> sortedWordList = getIntent().getParcelableArrayListExtra("sortedWordList");

My errors:

     01-19 16:26:33.044  16121-16121/com.example.harrisonhe.chatstats E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.harrisonhe.chatstats, PID: 16121
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.harrisonhe.chatstats/com.example.harrisonhe.chatstats.StatActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2439)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
        at android.app.ActivityThread.access$800(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5633)
        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:896)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at com.example.harrisonhe.chatstats.StatActivity.onCreate(StatActivity.java:49)
        at android.app.Activity.performCreate(Activity.java:5312)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
                    at android.app.ActivityThread.access$800(ActivityThread.java:153)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:157)
                    at android.app.ActivityThread.main(ActivityThread.java:5633)
                    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:896)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
                    at dalvik.system.NativeStart.main(Native Method)

The nullPointerException comes from when I try to access the ArrayList from getParcelableArrayListExtra.

Does anybody have any idea what's going on? I've tried every single solution I could find to this issue, to no avail. It's costing me a lot of time and any help would really be appreciated!

尝试这个

ArrayList<Occurrence> ppl =  getIntent().getExtras().getParcelableArrayList("ppl");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM