2012-01-17 119 views
1

我有一个getParcelableArrayListExtra和空指针异常的问题。getParcelableArrayListExtra返回nullpointerexception

工作

我的活动:

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    fetch = new ArrayList<Custom>(); 
    generateEntries(); 

    Log.i("fetch", fetch.toString()); 

    Intent myIntent = new Intent(this, CustomObject.class); 
    //myIntent.putParcelableArrayListExtra("my", fetch); 
    myIntent.putExtra("my", "name"); 
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(myIntent); 
} 

CustomObject:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.customobject); 
    lv = (ListView) findViewById(R.id.listview); 

    recievedList = new ArrayList<Custom>(); 

    in = getIntent(); 

    String s = bu.getString("my"); 

    Log.i("s", "s"); 
} 

不工作

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    fetch = new ArrayList<Custom>(); 
    generateEntries(); 

    Log.i("fetch", fetch.toString()); 

    Intent myIntent = new Intent(this, CustomObject.class); 
    myIntent.putParcelableArrayListExtra("my", fetch); 
    //myIntent.putExtra("my", "name"); 
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(myIntent); 
} 

CustomObject:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.customobject); 
    lv = (ListView) findViewById(R.id.listview); 

    recievedList = new ArrayList<Custom>(); 

    in = getIntent(); 

    recievedList = in.getParcelableArrayListExtra("my"); // NULL POINTER EXCEPTION 
} 

什么是ArrayList的问题呢?

有人帮我吗?

.............................................. .................................................. .....

public class Custom implements Parcelable { 

    private String alarmTitle; 
    private String alarmType; 
    private String alarmTime; 
    private String alarmDate; 
    private List<String> shortVakatName; 
    private List<String> vakatActive; 

    public Custom(String entry1, List<String> list1, List<String> list2, String entry3, String entry4, String entry5){ 

     this.shortVakatName = new ArrayList<String>(); 
     this.vakatActive = new ArrayList<String>(); 

     this.alarmTitle = entry1; 
     this.shortVakatName = list1; 
     this.vakatActive = list2; 
     this.alarmType = entry3; 
     this.alarmTime = entry4; 
     this.alarmDate = entry5; 
    } 

    private Custom(Parcel in){ 
     alarmTitle = in.readString(); 
     in.readStringList(shortVakatName); 
     in.readStringList(vakatActive); 
     alarmTime = in.readString(); 
     alarmDate = in.readString(); 
    } 

    public static final Parcelable.Creator<Custom> CREATOR = 
      new Parcelable.Creator<Custom>() { 

     public Custom createFromParcel(Parcel source) { 
      return new Custom(source); 
     } 

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

    }; 

    public int describeContents() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(alarmTitle); 
     dest.writeStringList(shortVakatName); 
     dest.writeStringList(vakatActive); 
     dest.writeString(alarmType); 
     dest.writeString(alarmTime); 
     dest.writeString(alarmDate); 
    } 
} 
+0

,你正在做一个'in.getString( “我”)''的替代in.getParcelableArrayListExtra( “我”)' – louiscoquio 2012-01-17 22:09:43

+0

错字错误。很抱歉 – Kolesar 2012-01-17 22:18:34

+0

你可以给我们的Parcelable方法你的类是否自定义?你已经实现了Parcelable.Creator(newArray &createFromParcel)像在文档中一样:http://developer.android.com/reference/android/os/Parcelable.html – louiscoquio 2012-01-17 22:20:54

回答

16

问题出现在拆包包裹来创建新对象时。具体而言,您拨打readStringList()。此方法旨在使用包裹中的数据填充现有对象,而不是创建新的对象。

意识到当包裹解包时,将根据Parcelable.CREATOR的定义调用以Parcel作为参数的构造函数,而不是其他参数化构造函数。因此,shortVakatNamevakatActive都没有被初始化为任何东西(它们是空指针)。

你可以做两件事情之一解决该问题,要么让包裹充气数据时建立List为您提供:

private Custom(Parcel in){ 
    alarmTitle = in.readString(); 
    shortVakatName = in.createStringArrayList(); 
    vakatActive = in.createStringArrayList(); 
    alarmType = in.readString(); 
    alarmTime = in.readString(); 
    alarmDate = in.readString(); 
} 

或者告诉Parcel有数据填充它之前创建的对象。

private Custom(Parcel in){ 
    shortVakatName = new ArrayList<String>(); 
    vakatActive = new ArrayList<String>(); 

    alarmTitle = in.readString(); 
    in.readStringList(shortVakatName); 
    in.readStringList(vakatActive); 
    alarmType = in.readString(); 
    alarmTime = in.readString(); 
    alarmDate = in.readString(); 
} 

还要注意,在这两个例子,我固定的阅读顺序,以符合您writeToParcel()方法(你缺少alarmType参数,当你通过Intent通过你的数据这将导致奇怪的结果。

HTH在你的代码

+0

我希望我可以多次投票,我一直坚持这一两天,我几乎放弃使用包裹,感谢您解释的概念,而不是只写一个工作代码。 – 2014-03-24 16:48:44

+0

thnx ..这对我有帮助 – 2014-03-31 13:17:41

相关问题