Android 检索自定义包裹时出现空指针异常
所以我一直在查看StackOverFlow,似乎找不到一个完全解决我问题的主题。许多人在尝试使用自定义包裹时遇到空指针异常问题。我见过一些问题通过简单的解决方案得到解决,比如在parcelable类中向CREATOR添加Static。然而,我没有找到任何东西来帮助我的事业,也没有对我的处境有所启发 当从名为MyPointsList的自定义类访问坐标的ArrayList时,我得到一个空指针异常。在传递MyPointsList之前,我可以访问MyPointsList中的ArrayList。在检索实例时,我会进行快速检查,以确保该实例不是空的,它不是空的,然后继续访问ArrayList,在那里我会得到空指针异常 为什么在访问MyPointsList中的ArrayList时,在接收类中接收到空指针异常 以下是以下类别的代码: MyPoint.javaAndroid 检索自定义包裹时出现空指针异常,android,arraylist,nullpointerexception,custom-attributes,parcelable,Android,Arraylist,Nullpointerexception,Custom Attributes,Parcelable,所以我一直在查看StackOverFlow,似乎找不到一个完全解决我问题的主题。许多人在尝试使用自定义包裹时遇到空指针异常问题。我见过一些问题通过简单的解决方案得到解决,比如在parcelable类中向CREATOR添加Static。然而,我没有找到任何东西来帮助我的事业,也没有对我的处境有所启发 当从名为MyPointsList的自定义类访问坐标的ArrayList时,我得到一个空指针异常。在传递MyPointsList之前,我可以访问MyPointsList中的ArrayList。在检索实例
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();
}
}
}
检索段
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还是新手
谢谢 我觉得自己像个傀儡,但如果有人无意中发现了这一点,下面是解决办法 从MyPoint中删除:
public void readFromParcel(Parcel in){
int count = in.dataAvail();
for(int i = 0; i < count; i++){
x = in.readInt();
i++;
y = in.readInt();
}
}
就这样。我把他在问题中的答案改成了这个答案,基本上是一个“我解决了”的帖子-
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());
}
}
public void readFromParcel(Parcel in){
int count = in.dataAvail();
for(int i = 0; i < count; i++){
x = in.readInt();
i++;
y = in.readInt();
}
}
public void readFromParcel(Parcel in){
x = in.readInt();
y = in.readInt();
}