检索自定义视图时出现Android ClassCastException
尝试使用findViewById()获取自定义View时,我遇到了一个问题。 否则,自定义视图将正确显示和运行。 不幸的是,我需要能够随意更改它显示的一些数据,因此必须完成。 我的XML看起来像这样: 我知道有时问题是人们不小心在XML中命名组件“View”而不是它们的子类。 在我的Activity的onCreate()方法中,我有这个: myChart=(Chart)findViewById(R.id.chart); //where myChart is an object of type Chart 抛出ClassCastException。 我决定尝试一下,然后将行更改为此,以查看是否仍然收到ClassCastException: View chart=(View)findViewById(R.id.chart); 这很好用,告诉我findViewById给我一个View没有问题。 但它不想给我一张图表。 就Chart类而言,它是View的一个非常简单的子类,看起来可以正常工作。 public class Chart extends View{ public Chart(Context context) { super(context); init(); } public Chart(Context context, AttributeSet attrs) { super(context, attrs); init(); } public Chart(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); […]