【问题标题】:Unexpected cast to textview; layout tag was relative layout意外转换为 textview;布局标签是相对布局
【发布时间】:2016-02-13 03:12:21
【问题描述】:

我是 Android 编程新手,但我遇到了一个我无法弄清楚的非常基本的问题。我正在尝试使用一系列相对布局制作数独应用程序。但是当我尝试在我的应用程序中更改其中一个 TextView 中的文本时,我收到了这个错误:

意外转换为 textview;布局标签是相对布局

这里的任何帮助将不胜感激!

这是产生错误的java代码:

private void message (){
    TextView targetTextView = (TextView) findViewById(R.id.TL_Top_Left_Box);
    targetTextView.setText("1");
}

这是底层的 XML(相当删节,因为完整的代码非常重复):

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="20dp"
    android:layout_gravity="center_horizontal"
    tools:context="com.example.android.sudoku.MainActivity">

<RelativeLayout
    android:id="@+id/Sudoku_Grid"
    android:layout_width="240dp"
    android:layout_height="240dp"
    android:background="@android:color/black"
    android:layout_centerHorizontal="true">

    <RelativeLayout
        android:id="@+id/Top_Left_Grid"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@android:color/black"
        android:layout_toLeftOf="@id/Top_Center_Grid">

        <RelativeLayout
            android:id="@+id/TL_Top_Center_Box"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_centerHorizontal="true"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textColor="@android:color/black"
                android:background="@android:color/white"
                android:layout_marginTop="1dp"
                android:layout_marginRight="1dp"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="1dp"
                android:textSize="17sp"
                android:gravity="center"
                android:text="0"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/TL_Top_Left_Box"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_toLeftOf="@id/TL_Top_Center_Box"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textColor="@android:color/black"
                android:background="@android:color/white"
                android:layout_marginTop="1dp"
                android:layout_marginRight="1dp"
                android:layout_marginBottom="1dp"
                android:textSize="17sp"
                android:gravity="center"
                android:text="0"/>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

<Button
    android:id="@+id/Up_Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/Sudoku_Grid"
    android:layout_marginTop="25dp"
    android:layout_centerHorizontal="true"
    android:textSize="20sp"
    android:onClick="Click_1"
    android:text="Up"/>

</RelativeLayout>

【问题讨论】:

  • 您选择的是相对布局的 id 而不是文本视图。将 id 分配给 textview 并在消息函数中使用该 id
  • 完全正确,非常感谢!答案很明显。

标签: android casting


【解决方案1】:

这是定义 id TL_Top_Left_Box 的地方:

<RelativeLayout
    android:id="@+id/TL_Top_Left_Box"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_toLeftOf="@id/TL_Top_Center_Box"
    android:orientation="horizontal">

您可能希望删除该 id 行并将其添加到该 RelativeLayout 包含的 TextView 中:

<TextView
    android:id="@+id/TL_Top_Left_Box"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="@android:color/black"
    android:background="@android:color/white"
    android:layout_marginTop="1dp"
    android:layout_marginRight="1dp"
    android:layout_marginBottom="1dp"
    android:layout_marginLeft="1dp"
    android:textSize="17sp"
    android:gravity="center"
    android:text="0"/>

【讨论】:

    【解决方案2】:

    您正在请求 ID 为 TL_Top_Left_Box 的视图并将其转换为 TextView。在布局中,具有该 id 的视图是一个相对布局。您不能将 RelativeLayout 强制转换为 TextView。

    如果您希望获得 TextView,请务必请求正确的 id。但是,如果您希望使用 RelativeLayout,则改为使用该类型创建一个变量并使用该类型进行强制转换。

    【讨论】:

    • 你说的很对,感谢你发现我的错误,太明显了!
    【解决方案3】:
    <**RelativeLayout**
                android:id="@+id/TL_Top_Center_Box"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_centerHorizontal="true"
                android:orientation="horizontal">
    

    RelativeLayout 不是 TextView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 2017-10-01
      • 2020-02-05
      • 1970-01-01
      相关资源
      最近更新 更多