我是triyn实现Parcle数据并通过Intent将其发送到其他类但是获取NULL对象我是Android中的新手可能有一些愚蠢的错误,如果是抱歉... 通过按钮单击
传递Cretaed对象和Intent
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);
}
}
}
Parceldata实现了pacelclass
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
答案 0 :(得分:1)
这必须是构造函数。您必须删除void
:
public ParcelData(Parcel in){
readFromParcel(in);
}
否则它将被视为一种方法而不被使用。
创作者必须看起来像这样:
public static final Parcelable.Creator<ParcelData> CREATOR = new Parcelable.Creator<ParcelData>() {
public ParcelData createFromParcel(Parcel source) {
return new ParcelData(source);
}
public ParcelData[] newArray(int size) {
return new ParcelData[size];
}
}
@Override
public ParcelData[] newArray(int size) {
return new ParcelData[size];
}
您也不需要默认构造函数。无论如何,变量都被初始化为这些值。而且我不知道它是否会导致框架出现问题。
//remove
public ParcelData() {
firstName=null ;
addedBy=0;
// guid=null;
}
在describeContents中,如果你使用hashCode可能会有所帮助:
@Override
public int describeContents() {
return hashCode();
}