[英]Java/Android - GettingClassCast exception casting a base class to a subclass to invoke the subclass methods
So I have a GeneralTemplate class and an Exercise class which extends GeneralTemplate. 因此,我有一个GeneralTemplate类和一个扩展GeneralTemplate的Exercise类。
As can be seen in the GeneralTemplate code I have included, each class extending it (of which there are several) contain an ArrayList 从我包含的GeneralTemplate代码中可以看出,扩展它的每个类(有几个类)都包含一个ArrayList
When I get to this line... 当我到达这条线...
Exercise chosenExercise = (Exercise) FitnessApp.routineList.get(chosenRoutinePosition).getItem(chosenWorkoutPosition).getItem(chosenExercisePosition);
I get the following error ClassCastExceptionjava.lang.ClassCastException: com.karibastudios.gymtemplates.GeneralTemplate cannot be cast to com.karibastudios.gymtemplates.Exercise
我收到以下错误
ClassCastExceptionjava.lang.ClassCastException: com.karibastudios.gymtemplates.GeneralTemplate cannot be cast to com.karibastudios.gymtemplates.Exercise
I don't understand why this is as Exercise is a subclass of GeneralTemplate? 我不明白为什么会这样,因为Exercise是GeneralTemplate的子类?
GeneralTemplate code: 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 ********************/
}
Exception code where attempt to cast GeneralTemplate to exercise to invoke methods 尝试强制转换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);
The Exercise class is very simple and extends GeneralTemplate with a few additional methods Exercise类非常简单,并使用其他一些方法扩展了GeneralTemplate
Been a bit of a stumbling block this, any help would be amazing. 这是一个绊脚石,任何帮助都将是惊人的。
Cheers 干杯
ClassCastExceptionjava.lang.ClassCastException: com.karibastudios.gymtemplates.GeneralTemplate cannot be cast to com.karibastudios.gymtemplates.Exercise
In Java you can only cast in one direction. 在Java中,您只能沿一个方向进行投射。 For example: a ListView is a View, but not all Views are ListViews (it could be a Button, a RelativeLayout, etc).
例如:ListView是一个View,但并非所有View都是ListView(它可以是Button,RelativeLayout等)。
So you can go from an Exercise to a GeneralTemplate, but not the other way. 因此,您可以从“练习”转到“ GeneralTemplate”,但不能相反。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.