dazhao
dazhao

Reputation: 83

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

Upvotes: 0

Views: 45

Answers (3)

dazhao
dazhao

Reputation: 83

i got it, i should close the TextView begain the

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

Upvotes: 0

jayeshsolanki93
jayeshsolanki93

Reputation: 2136

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

Upvotes: 1

Hamed Siaban
Hamed Siaban

Reputation: 1682

Probably the problem is with your tvDialog.

I think you defined it as a ViewGroup instead of TextView in your java code.

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

Upvotes: 0

Related Questions