我试图使用Parcelable类将Stack从一个活动提供给另一个活动。为此,我按以下方式定义了MyStack和MyVector3。然后将其包含在Model类中。
模型类
public class Model implements Parcelable{
public List<MyStack> surfaces;
public Info info;
public Model (){}
public Model(List<MyStack> surf){
surfaces = surf;
}
public void setInfo(Info i){
info=i;
}
public Info getInfo(){
return info;
}
public List<MyStack> getSurfaces(){
return this.surfaces;
}
public int numSurfaces(){
return surfaces.size();
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeTypedList(surfaces);
//Parcelable infoP = ((Parcelable) info);
//out.writeParcelable(infoP, 0);
}
public static final Parcelable.Creator<Model> CREATOR
= new Parcelable.Creator<Model>() {
public Model createFromParcel(Parcel in) {
return new Model(in);
}
public Model[] newArray(int size) {
return new Model[size];
}
};
private Model(Parcel in) {
surfaces = new ArrayList<MyStack>();
in.readTypedList(surfaces, MyStack.CREATOR);
//info = in.readParcelable(Info.class.getClassLoader());
}
public class Info{
String title;
String descr;
public Info(String t, String d){
title=t;
descr=d;
}
public Info(String t){
title=t;
}
public void setDescr(String d){
descr=d;
}
}
}
MyStack课程
public class MyStack implements Parcelable {
public Stack<MyVector3> stack;
public MyStack(MyStack ms){
this.stack=ms.stack;
}
public MyStack(Stack s){
stack= s;
}
public Stack getStack(){
return this.stack;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeTypedList(stack);
}
public static final Parcelable.Creator<MyStack> CREATOR
= new Parcelable.Creator<MyStack>() {
public MyStack createFromParcel(Parcel in) {
return new MyStack(in);
}
public MyStack[] newArray(int size) {
return new MyStack[size];
}
};
private MyStack(Parcel in) {
stack= new Stack<MyVector3>();
in.readTypedList(stack, MyVector3.CREATOR);
}
}
MyVector3类
public class MyVector3 extends Vector3 implements Parcelable {
public Vector3 vector;
public MyVector3(Vector3 v){
vector=v;
}
public MyVector3(double x, double y, double z){
vector= new Vector3(x,y,z);
}
public Vector3 getVector(){
return this.vector;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeDoubleArray(new double[]{
this.x,
this.y,
this.z});
}
public static final Parcelable.Creator<MyVector3> CREATOR
= new Parcelable.Creator<MyVector3>() {
public MyVector3 createFromParcel(Parcel in) {
return new MyVector3(in);
}
public MyVector3[] newArray(int size) {
return new MyVector3[size];
}
};
private MyVector3(Parcel in) {
double[] data = new double[3];
in.readDoubleArray(data);
this.x= data[0];
this.y= data[1];
this.z= data[2];
}
}
这是意图创建,模型填充良好,我从日志中正确获取所有值
Model model= new Model(surfaces);
Intent intent = new Intent(this, EditorPresenter.class);
intent.putExtra("model", model);
startActivity(intent);
我在哪里使用它
Intent intent = getIntent();
model= intent.getParcelableExtra("model");
MyStack[] sf = model.surfaces.toArray(new MyStack[model.numSurfaces()]);
//surf = new Stack();
surf = new Stack[model.numSurfaces()];
Log.i(TAG, "ss="+Integer.toString(sf.length)); //expected value
for(int s=0; s<sf.length; s++) {
Log.i(TAG, "s="+Integer.toString(s)); //expected value
Stack st= new Stack();
Log.i(TAG, "vv="+Integer.toString(sf[s].getStack().size())); //expected value
for(int v=0; v<sf[s].getStack().size(); v++) {
Log.i(TAG, "v="+Integer.toString(v) +sf[s].stack.elementAt(v).getVector().toString()); //NullPointerException
MyVector3 mv = (MyVector3) sf[s].stack.elementAt(v);
if(mv!=null) st.add(mv.getVector());
}
surf[s]=st;
}
但是当我尝试读取/写入Vector3值时,我得到一个NullPointerException。
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.rajawali3d.math.vector.Vector3.toString()' on a null object reference
问题可能出在Parcelable类中,直到我从intent中得到一个错误的模型(预期的面数和顶点数是正确的,向量值可能为null)。任何帮助都非常感谢。
答案 0 :(得分:2)
从parcelable读取后,您没有实例化Vector对象。变化
private MyVector3(Parcel in) {
double[] data = new double[3];
in.readDoubleArray(data);
this.x= data[0];
this.y= data[1];
this.z= data[2];
}
与
private MyVector3(Parcel in) {
double[] data = new double[3];
in.readDoubleArray(data);
this.x= data[0];
this.y= data[1];
this.z= data[2];
vector = new Vector3(x,y,z);
}
答案 1 :(得分:0)
您可以使用此工具http://www.parcelabler.com/从代码中生成Parcelable类,然后使用新生成的类重试。