【问题标题】:java.lang.ClassCastException: com.modelrenderer.MyVector3$1 cannot be cast to com.modelrenderer.MyVector3java.lang.ClassCastException:com.modelrenderer.MyVector3$1 不能转换为 com.modelrenderer.MyVector3
【发布时间】:2016-02-21 21:21:23
【问题描述】:

我正在尝试使用 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));
    for(int s=0; s<sf.length; s++) {
        Log.i(TAG, "s="+Integer.toString(s));
        Stack st= new Stack();
        Log.i(TAG, "vv="+Integer.toString(sf[s].getStack().size()));
        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;
    }

但我收到以下错误:

java.lang.ClassCastException: com.modelrenderer.MyVector3$1 cannot be cast to com.modelrenderer.MyVector3

我真的不知道问题出在哪里,但我对 Android 编程非常陌生,尤其是使用 Parcelable 类。非常感谢任何帮助。

编辑:我用当前状态更新了类

无论我对 Vector3 项目做什么,当前的错误都是 NullPointerException。

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.rajawali3d.math.vector.Vector3.toString()' on a null object reference

【问题讨论】:

    标签: java android vector casting parcelable


    【解决方案1】:

    错误消息中的$1 为您提供了有关问题所在的线索。引用 anonymous (inner) class 时使用此表示法 - 因为它是“即时”声明的,所以它没有自己的名称,编译器可以使用。

    在您的特定情况下,它看起来意味着您的 CREATOR 变量。因此,在您的 for 循环中,该项目是 Parcelable.Creator&lt;MyVector3&gt; 实例之一,但您试图将其转换为 MyVector3 实例,因此是 ClassCastException。您应该能够通过简单地调试和“检查”sf[s].stack.elementAt(s) 表达式的计算结果来确认这一点。 (或者,如果您没有使用允许您调试的 IDE,请使用日志语句或打印语句来输出该对象的类的名称。)

    建议:1)使用更好的命名。调用 ssfmv 等,并不能帮助其他人阅读您的代码。 (并且代码的读取次数将比您写入的次数多。)2)继续阅读encapsulation。学习使用访问器,而不是公开每个实例变量。

    【讨论】:

    • 感谢您的提示。似乎 Parcelable 类有问题,因为我应该在那里有一个 Vector3 而不是 Parcelable.Creator
    • 解决了最后一个问题,但无论我尝试对向量做什么,我都会得到java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.rajawali3d.math.vector.Vector3.toString()' on a null object reference。所以我可能从 Intent 中得到了一个错误的模型或缺少某些东西。
    • 再一次 - 您最好的选择是调试和单步执行您的代码,检查值以查看它在做什么。如果您要编写更多代码,这是您需要反复使用的一项基本技能。从您编辑的评论中,听起来您没有正确初始化向量,因此当您尝试对其调用方法时,引用为null。如果您可以将您的问题浓缩为minimal, complete, and verifiable example,它也可能会帮助您自己和这里的任何人试图帮助您。
    • 我刚刚更新了问题中的代码。我整天都在尝试使用 Logs,但问题可能出在 Parcelable 类中,因为我没有从意图中得到正确的模型。我不认为我可以最小化我给你的代码,因为这三个类都是意图所必需的。
    • 请不要更改问题上的目标帖子。如果我的回答有助于解决您最初的问题,请接受并针对您的新问题提出新问题
    猜你喜欢
    • 1970-01-01
    • 2015-05-11
    • 2018-02-26
    • 2019-04-16
    • 2018-02-10
    • 2023-03-22
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多