繁体   English   中英

Java / Android-GettingClassCast异常将基类强制转换为子类以调用子类方法

[英]Java/Android - GettingClassCast exception casting a base class to a subclass to invoke the subclass methods

因此,我有一个GeneralTemplate类和一个扩展GeneralTemplate的Exercise类。

从我包含的GeneralTemplate代码中可以看出,扩展它的每个类(有几个类)都包含一个ArrayList

当我到达这条线...

Exercise chosenExercise = (Exercise) FitnessApp.routineList.get(chosenRoutinePosition).getItem(chosenWorkoutPosition).getItem(chosenExercisePosition);

我收到以下错误ClassCastExceptionjava.lang.ClassCastException: com.karibastudios.gymtemplates.GeneralTemplate cannot be cast to com.karibastudios.gymtemplates.Exercise

我不明白为什么会这样,因为Exercise是GeneralTemplate的子类?

GeneralTemplate代码:

public class GeneralTemplate
{
    private String name;
    private ArrayList <GeneralTemplate> items;  // Generic items list

    // Super constructor for all subclasses
    public GeneralTemplate(String name)
    {
        this.setName(name);
        items = new ArrayList<GeneralTemplate>();
    }

    // Only sets will differ
    public void addItem(String newName)
    {
        items.add(new GeneralTemplate(newName));
    }   

    // Remove item at position
    public void removeItem(int position)
    {
        if (items.size() > 0 && position <= items.size())
        items.remove(position);
    }

    // Remove all items
    public void removeItems()
    {
        items.clear();
    }

    /* ****************** GETTERS AND SETTERS START ********************/

    // Get item
    public GeneralTemplate getItem(int position)
    {
        return items.get(position);     
    }

    // Set list of objects e.g Routines, Workouts, Exercises
    public void setItems(ArrayList <GeneralTemplate> items)
    {
        this.items = items;
    }

    // Return item list
    public ArrayList <GeneralTemplate> getItems()
    {
        return items;
    }

    // Return name
    public String getName()
    {
        return name;
    }

    // Set name
    public void setName(String name)
    {
        this.name = name;
    }
    /* ****************** GETTERS AND SETTERS END ********************/
}

尝试强制转换GeneralTemplate行使以调用方法的异常代码

chosenRoutinePosition = getIntent().getIntExtra("chosen_routine", 0);
chosenWorkoutPosition = getIntent().getIntExtra("chosen_workout", 0);
chosenExercisePosition = getIntent().getIntExtra("chosen_workout", 0);

// Navigates through the Routine List and gets the chosen routine
chosenExercise = (Exercise)FitnessApp.routineList.get(chosenRoutinePosition).getItem(chosenWorkoutPosition).getItem(chosenExercisePosition);

Exercise类非常简单,并使用其他一些方法扩展了GeneralTemplate

这是一个绊脚石,任何帮助都将是惊人的。

干杯

ClassCastExceptionjava.lang.ClassCastException: com.karibastudios.gymtemplates.GeneralTemplate cannot be cast to com.karibastudios.gymtemplates.Exercise

在Java中,您只能沿一个方向进行投射。 例如:ListView是一个View,但并非所有View都是ListView(它可以是Button,RelativeLayout等)。

因此,您可以从“练习”转到“ GeneralTemplate”,但不能相反。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM