2012-01-17 120 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

相關問題