dazhao dazhao - 6 months ago 12
Android Question

the TextView and ViewGroup, java.lang.RuntimeException:

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




the AS told me : java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup, I don't how to solve

Answer

The error is a result of having MyLetterView inside of the TextView. TextView cannot contain other UI elements. Make it to something like this:

<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"/>
Comments