如何使用intent传递全班

时间:2014-01-09 07:46:31

标签: java android android-intent

我试图使用intent将整个类对象传递给其他活动,但我得到空指针异常。

这是我的主要活动代码

LiveData ld=list.get(arg2);
ArrayList<LiveData> lis=new ArrayList<MainActivity.LiveData>();
lis.add(ld);
Intent in=new Intent(mContext, show_data.class);
in.putParcelableArrayListExtra("Data1", lis);
startActivity(in);

这是LiveData类

class LiveData implements Parcelable
{
    String file_id,time,name,file_image,file_name;
    int tot_likes,tot_share,tot_comment,user_like;
    Bitmap img;
    public LiveData() {
        // TODO Auto-generated constructor stub
    }
    public LiveData(Parcel in) {
        String[] data = new String[1];
        in.readStringArray(data);
    }
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub

    }

     public final Parcelable.Creator CREATOR = new Parcelable.Creator() {
            public LiveData createFromParcel(Parcel in) {
                return new LiveData(in);
            }

            public LiveData[] newArray(int size) {
                return new LiveData[size];
            }
        };

}

这就是我在第二项活动中所写的内容

   ArrayList<LiveData> data = getIntent().getParcelableArrayListExtra("Data1");

和我的logcat:

 01-09 13:03:31.814: E/AndroidRuntime(9301): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.redtelephone1/com.example.redtelephone1.show_data}: java.lang.NullPointerException
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.os.Handler.dispatchMessage(Handler.java:99)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.os.Looper.loop(Looper.java:123)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.app.ActivityThread.main(ActivityThread.java:4627)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at java.lang.reflect.Method.invokeNative(Native Method)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at java.lang.reflect.Method.invoke(Method.java:521)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at dalvik.system.NativeStart.main(Native Method)
 01-09 13:03:31.814: E/AndroidRuntime(9301): Caused by: java.lang.NullPointerException
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at java.lang.reflect.Field.getField(Native Method)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at java.lang.reflect.Field.get(Field.java:247)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.os.Parcel.readParcelable(Parcel.java:1872)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.os.Parcel.readValue(Parcel.java:1771)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.os.Parcel.readListInternal(Parcel.java:2017)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.os.Parcel.readArrayList(Parcel.java:1461)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.os.Parcel.readValue(Parcel.java:1792)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.os.Parcel.readMapInternal(Parcel.java:2008)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.os.Bundle.unparcel(Bundle.java:208)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.content.Intent.getParcelableArrayListExtra(Intent.java:3424)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at com.example.redtelephone1.show_data.onCreate(show_data.java:25)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 01-09 13:03:31.814: E/AndroidRuntime(9301):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

1 个答案:

答案 0 :(得分:1)

您没有正确实施Parceable

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
  dest.writeString(file_id);
  dest.writeString(name);
 //write other fields
}

在构造函数中,读取值:

 public LiveData(Parcel in) {
        file_id = in.readString();
        name = in.readString();
        //read other fields
}