0

我有一个扩展parceble的类classOne。我正在使用 classOne 类来解析 json 让我们说提要消息 json 这是我的代码

public class classOne implements Parcelable{
    public String createdAt         = null;
    public String description       = null;
    public String ownerName          = null;
   public ArrayList<classOne> repliesArrayList = null;

public classOne(JSONObject jObject){ 
createdAt = jObject.isNull("created_at") == true ? "" :jObject.optString("created_at");
description = jObject.isNull("description") == true ? "":      jObject.optString("description");
ownerName = jObject.isNull("name") == true ? "" : jObject.optString("name");

if (!jobject.isNull("messages")) {
     repliesArrayList = new ArrayList<classOne>();
      JSONArray repliesJsonArray = jobject.optJSONArray("messages");
      int count = repliesJsonArray.length() - 1;

   for (int repliesCount = count; repliesCount >= 0; repliesCount--) {
    classOne repliesMessageModel = new     classOne(repliesJsonArray.optJSONObject(repliesCount));
      repliesArrayList.add(repliesMessageModel);
     }
  }
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
  dest.writeString(createdAt);
  dest.writeString(description);    
      dest.writeString(ownerName);
      dest.writeTypedList(repliesArrayList);    
     }

    private void readFromParcel(Parcel in) {  
        createdAt = in.readString();
        description = in.readString();
    ownerName = in.readString();
    in.readTypedList(repliesArrayList, classOne.CREATOR);
    }


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

}

它工作正常,但是当我尝试将 repliesArrayList 从一个活动发送到另一个活动时出现错误

这是我将数组列表从一个类发送到另一个类的代码

  Intent intent7 = new Intent(getApplicationContext(), ParceActivity.class);
  intent7.putParcelableArrayListExtra("msg", replyList); 
  startActivity(intent7);

公共类 ParceActivity 扩展 Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);     
 System.out.println("----------msg|" +getIntent().getParcelableArrayListExtra("msg").size());
  }

}

我收到以下错误。

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
     E/AndroidRuntime( 8432):   at         android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime( 8432):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime( 8432):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime( 8432):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 8432):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 8432):    at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime( 8432):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 8432):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 8432):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime( 8432):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime( 8432):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 8432): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 8432):    at android.os.Parcel.readListInternal(Parcel.java:2237)
E/AndroidRuntime( 8432):    at android.os.Parcel.readList(Parcel.java:1531)

如果我发送任何其他数据,它会正常工作,但如果我发送 replayList,它会给我一个错误。我不知道出了什么问题我已经调试了这段代码,在将重播列表从活动一发送到另一个活动时值就在那里。这是因为重播列表具有相同的数据类型“classOne”???

4

1 回答 1

-1

从 readToParcel 和 writeToParcel 中删除 repliesArrayList。

于 2013-09-03T17:31:49.407 回答