在 layout/main.xml 中引用内部类视图时出错 [英] Error referencing an inner class View in layout/main.xml

查看:16
本文介绍了在 layout/main.xml 中引用内部类视图时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

呜呜呜……

我在我的活动中创建了一个视图的子类作为内部类.在我简单地从我的活动链接到这个视图之前:

I create a subclass of view as an inner class in my Activity. Before I simply linked to this view from my activity with:

setContentView(new CustomView(this));

没有问题.

但是,现在我的视图变得越来越复杂,因此我将其作为 FrameLayout 的一部分,以便我可以将其作为基本视图并在其顶部添加一个 Spinner 小部件.问题是,当我这样做时,我收到一个错误:

Now, however, my view is getting more complex so I am making it part of a FrameLayout so that I can make this the base view and add a Spinner widget on top of it. The problem is, when I do this I get an error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.grafightscratch.ochemmer/com.grafightscratch.ochemmer.MoleculeTablet}: android.view.InflateException: Binary XML file line #4: Error inflating class com.grafightscratch.ochemmer.MoleculeTablet.MoleculeTabletView
...
Caused by: android.view.InflateException: Binary XML file line #4: Error inflating class com.grafightscratch.ochemmer.MoleculeTablet.MoleculeTabletView
...
Caused by: java.lang.ClassNotFoundException: com.grafightscratch.ochemmer.MoleculeTablet.MoleculeTabletView in loader dalvik.system.PathClassLoader@43b74a28

所以 - 这个视图以前在我直接链接到它时工作过,但是当我尝试将它添加到 main.xml 文件中作为框架布局的一部分时,我得到了上述错误.我还尝试通过以下方式放入仅显示的布局:

So- this view worked before when I linked to it directly, but when I tried to add it in the main.xml file as part of a framelayout I got the above error. I also tried putting into a layout with only it being displayed via:

<com.grafightscratch.ochemmer.MoleculeTablet.MoleculeTabletView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/molecule_tablet_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

没有任何效果.我不断收到 InflateException/ClassNotFoundException 错误.它抱怨二进制 XML 文件中的第 3 行",如果它在谈论 main.xml,那是我已经三重检查的包声明.

Nothing works. I keep getting the InflateException/ClassNotFoundException errors. It complains about "line #3" in the binary XML file, and if it is talking about main.xml that is the package declaration which I have triple checked.

编辑我尝试使这个视图成为一个单独的类(即 - 不是内部类)并且它有效.经过一番搜索,我发现一些帖子说 xml 标签应该是这样的:

EDIT I tried making this view a separate class (ie- not an inner class) and it works. After some searching around I found some posts saying that the xml tag should look like this:

<com.grafightscratch.ochemmer.MoleculeTablet$MoleculeTabletView ...>

即,应使用美元符号将内部类与主类分开.但是,Eclipse 对此表示反对,称其为错误,并拒绝让我在那里使用该角色进行构建或部署.所以现在问题变成了:如何引用作为内部类的 View?

Ie, a dollar sign should be used to separate the innerclass from the main class. However, Eclipse barfs on this, calls it an error, and refuses to let me build or deploy with that character there. So now the question becomes: how does one reference a View that is an inner class?

推荐答案

对于内部类,语法变为:

For inner classes the syntax becomes:

<view class="com.grafightscratch.ochemmer.MoleculeTablet$MoleculeTabletView" />

原因是 $ 在 XML 标签中是非法字符.

The reason is that $ is an illegal character in XML tags.

这篇关于在 layout/main.xml 中引用内部类视图时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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