1

所以我一直在看 StackOverFlow,似乎找不到完全解决我问题的话题。许多人在尝试使用自定义 Parcelable 时遇到空指针异常问题。我已经看到通过将静态添加到 parcelable 类中的 CREATOR 等简单的解决方案解决了问题。但是,我没有找到任何可以帮助我的事业或对我的处境有所启发的东西。

从名为 MyPointsList 的自定义类访问坐标的 ArrayList 时,出现空指针异常。在通过 MyPointsList 之前,我可以访问 MyPointsList 中的 ArrayList。检索实例后,我快速检查以确保实例不为空,它不是,然后继续访问我得到空指针异常的 ArrayList。

为什么我在访问 MyPointsList 中的 ArrayList 时在接收类中收到空指针异常?

以下是以下类的代码:

MyPoint.java

public class MyPoint implements Parcelable {
    private int x;
    private int y;

    public MyPoint(){
        this.x = 0;
        this.y = 0;
    }

    public MyPoint(Parcel in){
        this.x = 0;
        this.y = 0;
        readFromParcel(in);
    }

    public void setPoint(int px, int py){
        this.x = px;
        this.y = py;
    }

    public int getX(){return this.x;}

    public int getY(){return this.y;}

    public static final Parcelable.Creator <MyPoint> CREATOR = new Parcelable.Creator <MyPoint> (){
        public MyPoint createFromParcel(Parcel source) {
            // TODO Auto-generated method stub
            return new MyPoint(source);
        }

        public MyPoint[] newArray(int size) {
            // TODO Auto-generated method stub
            return new MyPoint[size];
        }       
    };

    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(x);
        dest.writeInt(y);

    }

public void readFromParcel(Parcel in){
        int count = in.dataAvail();
        for(int i = 0; i < count; i++){
            x = in.readInt();
            i++;
            y = in.readInt();
        }
    }
}

MyPointsList.java

public class MyPointsList implements Parcelable {

    private ArrayList<MyPoint> arrList = new ArrayList<MyPoint>();
    private int myInt = 0;
    private String myString = null;

    public String getString(){return myString;}

    public void setString(String str){this.myString = str;}

    public MyPoint getMyPoint(int index){return this.arrList.get(index);}

    public ArrayList<MyPoint> getArrayList(){return arrList;}

    public void setArrayList(ArrayList<MyPoint> arr){this.arrList = arr;}

    public int getMyInt(){return myInt;}

    public void setMyInt(int newInt){this.myInt = newInt;}

    MyPointsList(){this.arrList = new ArrayList<MyPoint>();}

    public MyPointsList(Parcel in){
        myInt = in.readInt();
        myString = in.readString();
        arrList = new ArrayList<MyPoint>();
        in.readTypedList(arrList, MyPoint.CREATOR);
    }

    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        // TODO Auto-generated method stub
        out.writeInt(myInt);
        out.writeString(myString);
        out.writeTypedList(arrList);
    }

    public static final Parcelable.Creator<MyPointsList> CREATOR = new Parcelable.Creator<MyPointsList>(){

        public MyPointsList createFromParcel(Parcel source) {
            // TODO Auto-generated method stub
            return new MyPointsList(source);
        }

        public MyPointsList[] newArray(int size) {
            // TODO Auto-generated method stub
            return new MyPointsList[size];
        }   
    };
}

打包段:points 是 MyPoints 的 ArrayList

public void onClick(View v){
    MyPointsList object = new MyPointsList();
        object.setMyInt(100);
        object.setArrayList(points);
        Log.d("DEBUG", object.getMyPoint(5).getX() + " " + object.getMyPoint(5).getY() + " ");
        Intent goBack = new Intent(SignHereActivity.this, Menu.class);
        goBack.putExtra("parcel" , object);             
        SignHereActivity.this.startActivity(goBack);
    }
});

检索段

public void getIntentData(){
    Intent i = this.getIntent();
    if(i.hasExtra("parcel")){
        Bundle b = getIntent().getExtras();
        object = b.getParcelable("parcel");
        Log.d("Debug", object.getArrayList().get(5).getX() + " " + object.getArrayList().get(5).getY());
    }
}

如前所述,我一直在浏览我能找到的与我的问题相关的任何信息。我非常感谢任何输入和响应。对于我的代码中任何烦人的语法或令人困惑的部分,我深表歉意。作为一个整体,我对 java 和 Android 还是很陌生。

谢谢!

4

1 回答 1

0

我觉得自己像个傻瓜,但是如果其他人偶然发现了这一点,这就是解决方案。

从 MyPoint 中删除:

public void readFromParcel(Parcel in){
    int count = in.dataAvail();
    for(int i = 0; i < count; i++){
        x = in.readInt();
        i++;
        y = in.readInt();
    }
}

插入到 MyPoint:

public void readFromParcel(Parcel in){
    x = in.readInt();
    y = in.readInt();
}

就是这样。

于 2013-01-07T17:37:36.303 回答