在实现Parcelable </object>之后,无法通过intent传递ArrayList <object>

时间:2015-01-19 21:20:43

标签: java android android-intent arraylist parcelable

在网上查看并看到Parcelable是通过意图传递对象ArrayList的方法之后,我几乎以一种可能的方式实现了它,但仍然无法成功地将它从一个活动传递到另一个活动。

我想要传递的对象的类:

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);
}
}

将其置于意图中:

 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

(尝试)在我的下一个活动中接收它:

@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");

我的错误:

     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)

当我尝试从getParcelableArrayListExtra访问ArrayList时,nullPointerException来自。

有人知道发生了什么吗?我已经尝试了我能找到的每个解决方案,但无济于事。这花费了我很多时间,真的很感激任何帮助!

1 个答案:

答案 0 :(得分:0)

试试这个

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