Xamarin - 二进制XML文件行#1:错误导致类<unknown> </unknown>

时间:2014-08-13 02:03:10

标签: c# android xml xamarin

我目前正在使用C#在Xamarin中开发一个小型Android应用 我在模拟器上运行时会出现以下内容:
Screenshot of Xamarin error
选择第一个选项卡时会调用此行代码,默认情况下,这意味着一旦运行程序就会发生此错误。在我的一个布局文件中,似乎导致错误的XML片段是这样的:

android:background="@color/done"

这是Dialer.axml的第111行,其中的全部内容如下。在这里,我尝试为我的按钮引用color state list,以便颜色根据是否被触摸而改变。我知道这行会导致错误,因为从它所在的button标记中删除它会导致程序完美运行。以下是done.xml文件夹中color的代码:

<?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>

以下是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?我把它放在错误的文件夹中了吗?

提前致谢。

1 个答案:

答案 0 :(得分:2)

您的done.xml是颜色状态列表,但颜色状态列表不能用作背景。您需要使用state-list-drawable作为背景。

以下是颜色状态列表的状态列表可绘制版本:

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

android:background="@drawable/done"

您可以在此链接中找到类似的问题: How to set custom button state background color?