Xamarin - 二进制XML文件行#1:错误充气类&LT;&不明GT; [英] Xamarin - Binary XML file line #1: Error inflating class <unknown>
问题描述
我目前正在开发中Xamarin的小Android应用程序,使用C#。结果
下图显示了,当我在我的模拟器运行:结果
结果
选择第一个选项卡时被调用code的这条线,这是在默认情况下,这意味着只要我运行程序时出现此错误。这似乎是导致错误的XML片段是这样的,在我的布局文件之一:
I am currently developing a small Android app in Xamarin, using C#.
The following shows up when I run it on my emulator:
This line of code is called when the first tab is selected, which is by default, meaning that this error occurs as soon as I run the program. The XML snippet which seems to be causing the error is this, in one of my layout files:
android:background="@color/done"
这是在线路Dialer.axml 111,其全部如下。在这里,我想引用一个颜色状态列表我按钮,这样取决于它是否被触摸与否的颜色变化。我知道这行导致错误,因为从按钮删除它
标签,它在导致程序完美运行。下面是 done.xml
的code,在我的颜色
文件夹:
This is in line 111 of Dialer.axml, the entirety of which is below. Here, I am trying to reference a color state list for my button, so that the color changes depending on whether it is being touched or not. I know that this line causes the error, because removing it from the button
tag which it's in causes the program to run perfectly. Here is the code for done.xml
, in my color
folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#ff2222ff"/>
<item android:state_pressed="true" android:state_enabled="false" android:color="#ff4444ff"/>
<item android:state_enabled="false" android:color="#ff000000"/>
<item android:color="#ff0000ff"/>
</selector>
这里是code为 Dialer.axml
:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tableLayout1"
android:background="#2ec0ff">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:layout_marginBottom="40sp"
android:id="@+id/number"
android:editable="false"
android:singleLine="true"
android:scrollHorizontally="true"
android:gravity="right"
android:textColor="#fff" />
<TableRow
android:id="@+id/tableRow1"
android:layout_weight="1">
<Button
android:text="1"
android:layout_column="0"
android:id="@+id/button1"
android:layout_weight="1"
android:layout_height="match_parent" />
<Button
android:text="2"
android:layout_column="1"
android:id="@+id/button2"
android:layout_weight="1"
android:layout_height="match_parent" />
<Button
android:text="3"
android:layout_column="2"
android:id="@+id/button3"
android:layout_weight="1"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_weight="1">
<Button
android:text="4"
android:layout_column="0"
android:id="@+id/button4"
android:layout_weight="1"
android:layout_height="match_parent" />
<Button
android:text="5"
android:layout_column="1"
android:id="@+id/button5"
android:layout_weight="1"
android:layout_height="match_parent" />
<Button
android:text="6"
android:layout_column="2"
android:id="@+id/button6"
android:layout_weight="1"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_weight="1">
<Button
android:text="7"
android:layout_column="0"
android:id="@+id/button7"
android:layout_weight="1"
android:layout_height="match_parent" />
<Button
android:text="8"
android:layout_column="1"
android:id="@+id/button8"
android:layout_weight="1"
android:layout_height="match_parent" />
<Button
android:text="9"
android:layout_column="2"
android:id="@+id/button9"
android:layout_weight="1"
android:layout_height="match_parent" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_weight="1">
<Button
android:text="*"
android:layout_column="0"
android:id="@+id/buttonStar"
android:layout_weight="1"
android:layout_height="match_parent" />
<Button
android:text="0"
android:layout_column="1"
android:id="@+id/button0"
android:layout_weight="1"
android:layout_height="match_parent" />
<Button
android:text="#"
android:layout_column="2"
android:id="@+id/buttonPound"
android:layout_weight="1"
android:layout_height="match_parent" />
</TableRow>
<Button
android:text="Done"
android:id="@+id/buttonDone"
android:background="@color/done"
android:layout_width="match_parent"
android:layout_height="80sp"
android:textSize="35sp"
android:layout_margin="5sp" />
</TableLayout>
是什么原因造成的错误?我在引用 done.xml
错误?难道我把它放在错误的文件夹?
What is causing the error? Am I referencing done.xml
incorrectly? Did I place it in the wrong folder?
先谢谢了。
推荐答案
您done.xml是颜色状态列表,但颜色状态列表不能被用来作为背景。您需要使用状态列表抽拉背景。
Your done.xml is color state list but color state lists can not be used as backgrounds. You need to use state-list-drawable for background.
下面是你的颜色状态列表状态列表抽拉版本:
Here is the state-list-drawable version of your color state list:
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape android:shape="rectangle">
<solid android:color="#ff2222ff" />
</shape>
</item>
<item android:state_pressed="true" android:state_enabled="false">
<shape android:shape="rectangle">
<solid android:color="#ff4444ff" />
</shape>
</item>
<item android:state_enabled="false">
<shape android:shape="rectangle">
<solid android:color="#ff000000" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#ff0000ff" />
</shape>
</item>
</selector>
将这个变成一个XML文件在您绘制的文件,使您的布局按钮这样的XML:
Put this into an xml file in your drawable file and make your layout button xml like this:
android:background="@drawable/done"
您可以找到在这个环节类似的问题:
<一href=\"http://stackoverflow.com/questions/14105061/how-to-set-custom-button-state-background-color\">How设置自定义按钮状态的背景颜色?
You can find similar question in this link: How to set custom button state background color?
这篇关于Xamarin - 二进制XML文件行#1:错误充气类&LT;&不明GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!