在检索自定义视图的Andr​​oid ClassCastException异常 [英] Android ClassCastException upon retrieving Custom View

查看:335
本文介绍了在检索自定义视图的Andr​​oid ClassCastException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图抓住用findViewById()的自定义视图时,正好碰上一个问题。自定义视图否则显示并正常运行。不幸的是,我需要能够改变它显示随意一些数据,所以它有许多工作要做。

I've just run into an issue when attempting to grab a custom View using findViewById(). The custom view otherwise displays and operates correctly. Unfortunately, I need to be able to change some data it displays at will, so it has to be done.

我的XML看起来像这样:

My XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <com.TheProject.TheApp.Chart 
    android:id="@+id/chart"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  />

</LinearLayout>

我知道有时候问题是,人们意外地命名的XML,而不是自己的子类它的组件视图。

I know sometimes the issue is that people accidentally name the component "View" in the XML instead of their subclass of it.

在我活动的onCreate()方法中,我有这样的:

Inside my Activity's onCreate() method, I have this:

myChart=(Chart)findViewById(R.id.chart); //where myChart is an object of type Chart

这抛出一个ClassCastException。

That throws a ClassCastException.

我决定尝试一点点,而是改了行到此,看我还收到一个ClassCastException:

I decided to experiment a little and instead changed the line to this to see if I still recieved a ClassCastException:

View chart=(View)findViewById(R.id.chart);

这工作得很好,它告诉我,findViewById毫不费力地给了我一个视图。但它并不想给我一个图表。

This worked fine, which tells me that findViewById has no trouble giving me a View. But it doesn't want to give me a Chart.

至于图表类去,这是一个非常简单的视图子类,它似乎否则正常工作。

As far as the Chart class goes, it's a really simple subclass of View that appears to otherwise work correctly.

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);        
    init();     
}
    /* More stuff below like onDraw, onMeasure, setData, etc. */
}

我可能只是在做一些愚蠢的事。难道你们有什么想法?感谢您的帮助!

I'm probably just doing something stupid. Do you guys have any ideas? Thanks for your help!

推荐答案

只是出于好奇,在另一布局中使用与&LT将XML;包括&GT; 标签?

Just out of curiosity, is that xml being used in another layout with an <include> tag?

我刚刚这样一个问题,我们包括了viewflipper另一个XML布局

I had an issue just like this where we were including another xml layout in a viewflipper with

&LT;包括布局=@布局/ some_layout.xml&GT; findviewbyid 已得到错误的小部件。我只好裹在整个外部布局&LT;合并&GT;&LT; /合并方式&gt; 标签,使其能够正确地合并到基地布局

<include layout="@layout/some_layout.xml"> and findviewbyid was getting the wrong widgets. I had to wrap the whole external layout in <merge></merge> tags to allow it to merge correctly into the base layout.

试着改变你的布局,这一点,如果是被列入了。

Try changing your layout to this, if it's being included.

<?xml version="1.0" encoding="utf-8"?>
<merge>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <com.TheProject.TheApp.Chart 
    android:id="@+id/chart"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  />

</LinearLayout>
</merge>

编辑:查看此链接有关合并工作

这篇关于在检索自定义视图的Andr​​oid ClassCastException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆