私はParcleデータを実装し、インテントを介して他のクラスに送信することを試みていますが、Androidで初めてのNULLオブジェクトを取得しています。はい、申し訳ありませんが、愚かな間違いがあるかもしれません....作成されたオブジェクトとインテントがボタンクリックで渡されました
public class ParceableExampleActivity extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
ParcelData p = new ParcelData();
p.setFirstName("Kumar");
p.setAddedBy(3);
Intent n = new Intent(this,ResultActivity.class);
n.putExtra("data", p);
startActivity(n);
}
}
}
pacelclass を実装する Parceldata
public class ParcelData implements Parcelable {
private String firstName ;
private int addedBy ;
//private UUID guid ;
private static final String TAG = "TEST" ;
public ParcelData() {
firstName=null ;
addedBy=0;
// guid=null;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getAddedBy() {
return addedBy;
}
public void setAddedBy(int addedBy) {
this.addedBy = addedBy;
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Parcelable createFromParcel(Parcel in) {
return new ParcelData();
}
@Override
public Parcelable[] newArray(int size) {
return new Parcelable[size];
}
} ;
public void writeToParcel(Parcel dest, int flags) {
Log.v(TAG, "writeToParcel..."+ flags);
dest.writeString(firstName);
dest.writeInt(addedBy);
}
public void ParcelData(Parcel in){
readFromParcel(in);
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
private void readFromParcel(Parcel in) {
firstName= in.readString();
addedBy= in.readInt();
}
}
オブジェクトの受け取り
public class ResultActivity extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button2:
Bundle b = getIntent().getExtras();
ParcelData result_p=b.getParcelable("data");
Log.v("RESULT", "result of Parcel..."+ result_p.getFirstName());
Log.v("RESULT", "result of Parcel..."+ result_p.getAddedBy());
}
}
}
しかし、ログの結果は NULL です