2016-02-13 79 views
-3

我是Android编程的新手,我有一个非常基本的问题,我无法弄清楚。我正在尝试使用一系列相关布局来制作Sudoku应用程序。但是,当我试图改变在我的应用程序的TextViews的一个文本,我得到这个错误:意外转换为textview;布局标记是相对布局

Unexpected cast to textview; layout tag was relative layout

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

这是产生错误的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> 
+1

您正在选择相对布局的ID而不是文本视图。将id分配给textview并在消息函数中使用该id – Peshal

+0

正是如此,非常感谢!答案非常明显。 – Tennisfan42

回答

0

您所要求的使用ID TL_Top_Left_Box查看并将其转换为TextView。在布局中,具有该ID的视图是一个RelativeLayout。您不能将RelativeLayout投射到TextView。

如果你期望得到一个TextView,那么一定要请求正确的ID。但是,如果你期待着一个RelativeLayout,那就用这个类型创建一个变量,然后使用该类型进行转换。

+0

你绝对是对的,谢谢你抓住我的错误,非常明显! – Tennisfan42

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

当然,如此明显!感谢您捕捉我的错误,非常感谢! – Tennisfan42

0
<**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