2016-05-29 86 views
0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#f0f0f0"> 

<TextView 
    android:id="@+id/tv_dialog" 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:layout_centerInParent="true" 
    android:background="@android:color/darker_gray" 
    android:gravity="center" 
    android:textColor="#ffffffff" 
    android:textSize="30dp" 
    android:visibility="gone"> 

    <com.myspace.maoyannew.view.MyLetterView 
     android:id="@+id/my_letterview" 
     android:layout_width="25dp" 
     android:layout_height="match_parent" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="2dp" 
     android:layout_marginTop="7dp"> 

    </com.myspace.myspace.view.MyLetterView> 
</TextView> 

TextView的和ViewGroup中,了java.lang.RuntimeException:

的AS告訴我:java.lang.ClassCastException:android.widget.TextView不能轉換到android.view.ViewGroup,我不如何解決

+0

myLetterView =(MyLetterView)findViewById(R.id.my_letterview); tvDialog =(TextView)findViewById(R.id.tv_dialog); – dazhao

+0

java.lang.ClassCastException:android.widget.TextView無法轉換爲android.view.ViewGroup – dazhao

回答

1

該錯誤是TextView內部有MyLetterView的結果。 TextView不能包含其他UI元素。 使其向這樣的事情:

<TextView 
    android:id="@+id/tv_dialog" 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:layout_centerInParent="true" 
    android:background="@android:color/darker_gray" 
    android:gravity="center" 
    android:textColor="#ffffffff" 
    android:textSize="30dp" 
    android:visibility="gone"/> 

<com.myspace.maoyannew.view.MyLetterView 
    android:id="@+id/my_letterview" 
    android:layout_width="25dp" 
    android:layout_height="match_parent" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="2dp" 
    android:layout_marginTop="7dp"/> 
+1

是的,這是我的錯誤 – dazhao

0

也許問題是在你tvDialog。

我想你在java代碼中將它定義爲ViewGroup而不是TextView。

TextView tvDialog = findViewById(R.id.tv_dialog) ; 
+0

謝謝,樓上的答案是 – dazhao

0

我得到它,我應該關閉的TextView begain的

<TextView 
android:id="@+id/tv_dialog" 
android:layout_width="80dp" 
android:layout_height="80dp" 
android:layout_centerInParent="true" 
android:background="@android:color/darker_gray" 
android:gravity="center" 
android:textColor="#ffffffff" 
android:textSize="30dp" 
android:visibility="gone"/> 

<com.myspace.maoyannew.view.MyLetterView 
    android:id="@+id/my_letterview" 
    android:layout_width="25dp" 
    android:layout_height="match_parent" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="2dp" 
    android:layout_marginTop="7dp"> 

</com.myspace.myspace.view.MyLetterView> 
相關問題