Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android getParcelableArrayListExtra返回nullpointerexception_Android_Nullpointerexception_Parcelable - Fatal编程技术网

Android getParcelableArrayListExtra返回nullpointerexception

Android getParcelableArrayListExtra返回nullpointerexception,android,nullpointerexception,parcelable,Android,Nullpointerexception,Parcelable,我对getParcelableArrayListExtra和空指针异常有问题 工作 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); fetch = new ArrayL

我对getParcelableArrayListExtra和空指针异常有问题

工作

/** 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);
}
我的活动:

/** 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);
}
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fetch=newarraylist();
generateEntries();
Log.i(“fetch”,fetch.toString());
Intent myIntent=新的Intent(这个,CustomObject.class);
//myIntent.putParcelableArrayListExtra(“我的”,获取);
myIntent.putExtra(“我的”、“姓名”);
myIntent.setFlags(Intent.FLAG\u活动\u新任务);
星触觉(myIntent);
}
自定义对象:

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");
}
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
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.customobject);
lv=(ListView)findViewById(R.id.ListView);
ReceivedList=new ArrayList();
in=getIntent();
字符串s=bu.getString(“我的”);
日志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);
}
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fetch=newarraylist();
generateEntries();
Log.i(“fetch”,fetch.toString());
Intent myIntent=新的Intent(这个,CustomObject.class);
myIntent.putParcelableArrayListExtra(“我的”,获取);
//myIntent.putExtra(“我的”、“姓名”);
myIntent.setFlags(Intent.FLAG\u活动\u新任务);
星触觉(myIntent);
}
自定义对象:

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");
}
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
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.customobject);
lv=(ListView)findViewById(R.id.ListView);
ReceivedList=new ArrayList();
in=getIntent();
ReceivedList=in.getParcelableArrayListExtra(“my”);//空指针异常
}
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);
    }
}
public类自定义实现可包裹{
私有字符串标题;
私有字符串报警类型;
私有字符串报警时间;
私有字符串报警日期;
私人名单姓名;
私有列表激活;
公共自定义(字符串entry1、列表list1、列表list2、字符串entry3、字符串entry4、字符串entry5){
this.shortVakatName=new ArrayList();
this.vakatActive=新的ArrayList();
this.alarmTitle=entry1;
this.shortVakatName=list1;
this.vakatActive=list2;
this.alarmType=entry3;
this.alarmTime=entry4;
this.alarmDate=entry5;
}
私人定制(包裹内){
alarmTitle=in.readString();
in.readStringList(shortVakatName);
in.readStringList(vakatActive);
alarmTime=in.readString();
alarmDate=in.readString();
}
公共静态最终包裹。创建者=
新建Parcelable.Creator(){
公共自定义createFromParcel(地块源){
返回新的自定义(源);
}
公共自定义[]新数组(整数大小){
返回新的自定义[大小];
}
};
公共int描述内容(){
//TODO自动生成的方法存根
返回0;
}
公共无效写入包裹(包裹目的地,内部标志){
目的地记录(报警标题);
目的地记录列表(shortVakatName);
目标写入列表(激活);
目的写入限制(报警类型);
目的写入限制(报警时间);
目的写入限制(报警日期);
}
}

解包地块以创建新对象时会出现问题。具体地说,您可以调用
readStringList()
。此方法旨在使用地块中的数据填充现有对象,而不是创建新对象

请注意,当包裹被解包时,将根据
Parcelable.CREATOR
的定义调用以
包裹
为参数的构造函数,而不是其他参数化构造函数。因此,
shortVakatName
vakatActive
都没有初始化为任何内容(它们是空指针)

您可以通过执行以下两种操作之一来解决此问题,即在膨胀数据时让包裹为您创建
列表

private Custom(Parcel in){
    alarmTitle = in.readString();
    shortVakatName = in.createStringArrayList();
    vakatActive = in.createStringArrayList();
    alarmType = in.readString();
    alarmTime = in.readString();
    alarmDate = in.readString();
}
或者,在告诉
地块
用数据填充地块之前创建对象

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();
}
私人定制(包裹入内){
shortVakatName=新的ArrayList();
vakatActive=新的ArrayList();
alarmTitle=in.readString();
in.readStringList(shortVakatName);
in.readStringList(vakatActive);
alarmType=in.readString();
alarmTime=in.readString();
alarmDate=in.readString();
}
另外请注意,在这两个示例中,我固定了读取顺序以匹配
writeToParcel()
方法(您缺少
alarmType
参数,当您通过
Intent
传递数据时,这会导致奇怪的结果)


HTH

解包包裹以创建新对象时会出现问题。具体来说,就是调用
readStringList()
。此方法旨在使用包裹中的数据填充现有对象,而不是创建新对象

请注意,当包裹被解包时,根据
Parcelable.CREATOR
的定义,将调用以
包裹
为参数的构造函数,而不是其他参数化构造函数。因此,
shortVakatName
vakaactive
都没有初始化为任何内容(它们是空指针)

您可以通过执行以下两种操作之一来解决此问题,即在膨胀数据时让包裹为您创建
列表

private Custom(Parcel in){
    alarmTitle = in.readString();
    shortVakatName = in.createStringArrayList();
    vakatActive = in.createStringArrayList();
    alarmType = in.readString();
    alarmTime = in.readString();
    alarmDate = in.readString();
}
或者,在告诉
地块之前创建对象