Android 在 layout/main.xml 中引用内部类视图时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2098318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:32:20  来源:igfitidea点击:

Error referencing an inner class View in layout/main.xml

androidlayoutview

提问by IcedDante

Grrr...

咕噜噜...

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));

without problems.

没有问题。

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:

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

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

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:

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

<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"
/>

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.

什么都行不通。我不断收到 InflateException/ClassNotFoundException 错误。它抱怨二进制 XML 文件中的“第 3 行”,如果它在谈论 main.xml,那是我已经三重检查的包声明。

EDITI 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:

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

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

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?

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

回答by Romain Guy

For inner classes the syntax becomes:

对于内部类,语法变为:

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

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

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

回答by Whatzit Toya

I was having the same issue. The syntax in the XML file was correct, however.

我遇到了同样的问题。但是,XML 文件中的语法是正确的。

What ended up resolving the issue for me was that the inner class needs to be declared as static. For example:

最终为我解决问题的是内部类需要声明为静态。例如:

public static class myWebView extends WebView

回答by Adnan Abdollah Zaki

for inner class :

对于内部类:

<view class="{package}.{ParentClass}${innerClass}" />

and for inner class , you must declare your class :

对于内部类,您必须声明您的类:

public static InnerClass


staticis require .

static是需要的。

回答by dianakarenms

Here are some key points to make a custom view inside an inner class...

以下是在内部类中创建自定义视图的一些关键点......

public static class MainClass {
    ....
    ....
        public class SubClassView extends LinearLayout {
           public SubClassView(Context context, AttributeSet attrs) {
                super(context, attrs);
                .....
           }
    ....
    ....
       }
    }

The layout should be as follows:

布局应如下所示:

<view class = ".MainClass$SubClassView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/button"/>

Java class

Java类

  • staticis required
  • constructorwith AttributeSet is required (at least one)
  • 静态是必需的
  • 需要具有 AttributeSet 的构造函数(至少一个)

XML file

XML文件

  • viewtag (with lower case NOT View) is required
  • class tagwith the path to your inner class, using
  • $instead of "." before your SubClassView name
  • 需要查看标签(小写的 NOT View)
  • 带有内部类路径的类标记,使用
  • $而不是“.” 在您的 SubClassView 名称之前

回答by justin

<view xmlns:android="http://schemas.android.com/apk/res/android"
    class="com.example.Myproject.Myactivity$Myview"
     android:layout_width="fill_parent" android:id="@+id/name" android:visibility="visible" android:layout_gravity="bottom" android:layout_height="fill_parent" android:focusableInTouchMode="true"
/>

this code worked for me. When i left out some of the elements like layout_width my program crashed. I also had to make my view class static in order for it to work. In the end It would have been the same if i just took it out of its nest. The android note example uses a nested class.

这段代码对我有用。当我遗漏了一些像 layout_width 这样的元素时,我的程序崩溃了。我还必须将我的视图类设为静态才能使其正常工作。最后,如果我只是把它从它的窝里拿出来,它会是一样的。android note 示例使用嵌套类。

回答by Samuh

You need to specify the fully qualified name of your view class in the XML for inflation to work and View Class to be found by the Runtime System.
Since you have declared your View as inner class of your activity the fully qualified name would be: <your_package_name>.OuterClassName.InnerClassName

您需要在 XML 中指定您的视图类的完全限定名称,以便膨胀工作和运行时系统找到的视图类。
由于您已将 View 声明为活动的内部类,因此完全限定名称将是:<your_package_name>.OuterClassName.InnerClassName

Are you sure com.grafightscratch.ochemmer.CustomViewis the fully qualified name of your class?

您确定com.grafightscratch.ochemmer.CustomView是您班级的完全限定名称吗?

EDIT: Thanks for reminding me of this. When the views are declared as nested classes there is a slight aberration, see Use Custom componentof this document.

编辑:谢谢你提醒我这一点。当视图被声明为嵌套类时,会有轻微的偏差,请参阅本文档的使用自定义组件