2012-01-17 6 views
1

getParcelableArrayListExtra 및 Null 포인터 예외에 문제가 있습니다.getParcelableArrayListExtra return 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.getParcelableArrayListExtra'의'in.getString ("내") ("내")' – louiscoquio

+0

오타 오류를하고 있습니다. 죄송합니다 – Kolesar

+0

당신이 우리의 Parcelable 방법을 줄 수 당신의 클래스 Custom? 당신은 Parcelable.Creator (newArray & createFromParcel) doc : http://developer.android.com/reference/android/os/Parcelable.html – louiscoquio

답변

16

문제는 새 개체를 만들기 위해 구획을 풀 때 발생합니다. 특히 readStringList()으로 전화하십시오. 이 메서드는 기존 객체를 새 객체를 만들지 않기 위해 소포의 데이터로 채우도록 설계되었습니다.

파셀이 압축 해제 될 때 Parcelable.CREATOR의 정의에 따라 Parcel을 매개 변수로 사용하는 생성자가 호출되며 다른 매개 변수화 된 생성자는 호출되지 않습니다. 따라서 shortVakatNamevakatActive도 초기화되지 않았습니다 (null 포인터 임).

당신은 두 가지 중 하나를 수행하여 문제를 해결, 하나의 데이터 팽창 할 때 소포는 당신을위한 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() 방법에 맞게 읽기 순서 (당신이 Intent를 통해 데이터를 전달하면 이상한 결과를 이끌어했을 alarmType 매개 변수를 누락 한 고정, 알 수 있습니다.

HTH 코드에서

+0

나는 이것을 한 번 이상 투표 할 수 있으면 좋겠다. 며칠 동안이 일에 매달 렸고 필자는 소포를 사용하지 않았다. 단지 작업 코드를 작성하는 대신 개념을 설명해 주셔서 감사합니다. –

+0

thnx ..이 도움이되었습니다. –