getParcelableArrayListExtra返回NullPointerException异常 [英] getParcelableArrayListExtra return nullpointerexception

查看:1972
本文介绍了getParcelableArrayListExtra返回NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有getParcelableArrayListExtra和空指针异常的问题。

工作

我的活动:

  / **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    取=新的ArrayList<定制>();
    generateEntries();    Log.i(取,fetch.toString());    意图myIntent =新意图(这一点,CustomObject.class);
    //myIntent.putParcelableArrayListExtra(\"my,取);
    myIntent.putExtra(我的,名);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(myIntent);
}

CustomObject:

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.customobject);
    LV =(ListView控件)findViewById(R.id.listview);    recievedList =新的ArrayList<定制>();    在= getIntent();    字符串s = bu.getString(我);    Log.i(S,S);
}

不工作

  / **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    取=新的ArrayList<定制>();
    generateEntries();    Log.i(取,fetch.toString());    意图myIntent =新意图(这一点,CustomObject.class);
    myIntent.putParcelableArrayListExtra(我的,取);
    //myIntent.putExtra(\"my,名);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(myIntent);
}

CustomObject:

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.customobject);
    LV =(ListView控件)findViewById(R.id.listview);    recievedList =新的ArrayList<定制>();    在= getIntent();    recievedList = in.getParcelableArrayListExtra(我); // 空指针异常
}

什么是ArrayList的问题?

有人帮帮我吗?

.....................................................................................................

 公共类定制实现Parcelable {    私人字符串alarmTitle;
    私人字符串alarmType;
    私人字符串alarmTime;
    私人字符串alarmDate;
    私人列表<串GT; shortVakatName;
    私人列表<串GT; vakatActive;    市民自定义(字符串取值范,列表与LT;弦乐> list1的,列表与LT;弦乐>列表2,串entry3,字符串entry4,字符串entry5){        this.shortVakatName =新的ArrayList<串GT;();
        this.vakatActive =新的ArrayList<串GT;();        this.alarmTitle =取值范;
        this.shortVakatName = list1的;
        this.vakatActive =列表2;
        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<定制> CREATOR =
            新Parcelable.Creator<定制>(){        公共定制createFromParcel(包裹源){
            返回新的自定义(源);
        }        市民自定义[] newArray(INT大小){
            返回新的自定义【尺寸】;
        }    };    公众诠释describeContents(){
        // TODO自动生成方法存根
        返回0;
    }    公共无效writeToParcel(DEST包裹,INT标志){
        dest.writeString(alarmTitle);
        dest.writeStringList(shortVakatName);
        dest.writeStringList(vakatActive);
        dest.writeString(alarmType);
        dest.writeString(alarmTime);
        dest.writeString(alarmDate);
    }
}


解决方案

当你打开包装的包裹创建一个新的对象,问题就来了。具体来说,您的通话 readStringList()。此方法被设计成从包裹填充数据的现有对象,而不是建立一个新的。

意识到,当包裹被打开包装,带有一个包裹构造作为参数被调用,按你的 Parcelable的定义。 CREATOR ,而不是其他参数的构造函数。因此,无论 shortVakatName 也不 vakatActive 曾经被初始化为任何东西(它们是空指针)。

您可以通过做两件事情之一解决该问题,要么让包裹创建列表为你充气数据时:

 私人定制(包裹中){
    alarmTitle = in.readString();
    shortVakatName = in.createStringArrayList();
    vakatActive = in.createStringArrayList();
    alarmType = in.readString();
    alarmTime = in.readString();
    alarmDate = in.readString();
}

或者包裹之前告诉创建对象用数据来填充它。

 私人定制(包裹中){
    shortVakatName =新的ArrayList<串GT;();
    vakatActive =新的ArrayList<串GT;();    alarmTitle = in.readString();
    in.readStringList(shortVakatName);
    in.readStringList(vakatActive);
    alarmType = in.readString();
    alarmTime = in.readString();
    alarmDate = in.readString();
}

另请注意,在这两个例子,我固定的阅读顺序来匹配你的 writeToParcel()办法(你失踪了 alarmType 参数,当你穿过你的数据这将导致奇怪的结果的意图

心连心

I have a problem with getParcelableArrayListExtra and Null Pointer Exception.

Working

My Activity:

/** 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");
}

NOT working

/** 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
}

What is the problem with ArrayList?

Anybody help me?

.....................................................................................................

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);
    }
}

解决方案

The problem comes when you are unpacking the parcel to create a new object. Specifically, your calls to readStringList(). This method is designed to fill an existing object with data from the parcel, not to create a new one.

Realize that when the parcel is unpacked, the constructor that takes a Parcel as a parameter is being called, per the definition of your Parcelable.CREATOR, and NOT the other parameterized constructor. Therefore, neither shortVakatName nor vakatActive were ever initialized to anything (they are null pointers).

You can fix the issue by doing one of two things, either let the Parcel create the List for you when inflating the data:

private Custom(Parcel in){
    alarmTitle = in.readString();
    shortVakatName = in.createStringArrayList();
    vakatActive = in.createStringArrayList();
    alarmType = in.readString();
    alarmTime = in.readString();
    alarmDate = in.readString();
}

Or, create the objects before telling Parcel to fill it with data.

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();
}

Also notice, in both examples, I fixed the reading order to match your writeToParcel() method (you were missing the alarmType parameter, which would have led to strange results when you passed your data through the Intent.

HTH

这篇关于getParcelableArrayListExtra返回NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆