通过Intent传递后获取null对象。我的对象类实现了Parcelable接口。

时间:2014-11-10 10:36:48

标签: java android android-intent nullpointerexception parcelable

活动1,从我发送我的Parcelable对象:

Intent intent = new Intent(JobFieldAttribute.this, JobCamera.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle mBundle = new Bundle();                     
mBundle.putParcelable("FieldData", fieldData); 
mBundle.putSerializable("JobTransaction",jobTransaction);
mBundle.putString("imgPos", 0+"");
intent.putExtra("parce",mBundle);
startActivity(intent);

我的对象类:

public class FieldData implements Parcelable {
private Integer id;
private String value;
private Integer job_transaction_id;
private Integer field_attribute_master_id;
private Boolean required;
private View view;
private String viewType;
private String viewLabel;
private String viewSubLabel;
private String viewHelpText;

//All Getter setter methods

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(0);
    dest.writeString(value);
    try{
        dest.writeInt(job_transaction_id);
    }
    catch(Exception e){dest.writeInt(0);}
    dest.writeInt(field_attribute_master_id);
    try{
        dest.writeByte((byte) (required ? 1 : 0));
    }
    catch(Exception e){dest.writeByte((byte)0);}
    try{
        dest.writeString(viewType);
        }
    catch(Exception e){dest.writeString("NOVIEWTYPE");}
    try{
        dest.writeString(viewLabel);
        }
    catch(Exception e){dest.writeString("NO LABEL");}
    try{
        dest.writeString(viewSubLabel);
        }
    catch(Exception e){dest.writeString("NO SUB LABEL");}
    try{
        dest.writeString(viewHelpText);
        }
    catch(Exception e){dest.writeString("NO HELP TEXT");}
}   

public static final Parcelable.Creator<FieldData> CREATOR = new Parcelable.Creator<FieldData>()  {
    @Override
    public FieldData createFromParcel(Parcel in) {
        FieldData fieldData = new FieldData();
        fieldData.id = in.readInt();
        Log.i("Pracel in", ">> " + in.readString());
        fieldData.value = in.readString();
        fieldData.job_transaction_id = in.readInt();
        fieldData.field_attribute_master_id = in.readInt();           
        fieldData.required = in.readByte() != 0;
        fieldData.viewType = in.readString();
        fieldData.viewLabel = in.readString();
        fieldData.viewSubLabel = in.readString();
        fieldData.viewHelpText = in.readString();
        return fieldData;
    }

    @Override
    public FieldData[] newArray(int size) {
        return new FieldData[size];
    }
};

}

我的第二个活动,它接收此Parcelable对象:

super.onCreate(savedInstanceState); 
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_job_camera);
FieldData fieldData = (FieldData) getIntent().getParcelableExtra("fieldData");

当我打印我的fieldData对象时,我得到一个NullPointerException。我无法理解这个错误的原因。 我究竟做错了什么? 我是android开发的新手,使用Parcelable接口通过Intent传递对象。 请帮忙。

2 个答案:

答案 0 :(得分:1)

您将Bundle添加到Intent个额外内容中,而不是直接FieldData。因此,您的代码应如下所示:

getIntent().getBundleExtra("parce").getParcelableExtra("fieldData");

答案 1 :(得分:1)

您正在放FieldData,但试图返回fieldData。将它与@Dmitry Zaitsev的答案结合起来getIntent().getBundleExtra("parce").getParcelableExtra("FieldData");