【问题标题】:android.widget.TextView cannot be cast to android.view.ViewGroup0android.widget.TextView 无法转换为 android.view.ViewGroup0
【发布时间】:2015-09-21 12:42:12
【问题描述】:

找不到此错误的来源。 看到其他关于同一问题的问题,这通常是由于缺少结束标签引起的。但是找不到我的代码中缺少哪一个...

我的主要activity.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="horizontal">

<TextView
    android:text="@string/main_textview"
    android:layout_width="0px"
    android:layout_weight="2"
    android:background="#FFF8DC"
    android:padding="@dimen/component_padding"
    android:layout_height="match_parent" >
</TextView>

<TextView
    android:id="@+id/simplefragment"
    android:layout_width="0px"
    android:layout_weight="2"
    android:orientation="vertical"
    android:padding="@dimen/component_padding"
    android:layout_height="match_parent" >
</TextView>

</LinearLayout>

还有一个片段:

<TextView
    android:text="@string/hello_world"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</TextView>

</LinearLayout>

Logcat 说:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.melle.androidfragments/com.example.melle.androidfragments.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup

 Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup

【问题讨论】:

  • 您是否尝试将 TextView 用作片段的容器?
  • @Blackbelt 确实我想将它用作片段的容器,我做错了什么?
  • 不要使用 TextView。使用 FrameLayout。
  • 我应该编辑哪一个?

标签: android android-layout android-studio android-widget textview


【解决方案1】:

Viewgroup 表示可以在其中包含其他视图的视图。你分别打开和关闭你的 TextViews,当他们不能的时候把它们变成一个组。

根据您发布的代码,解决方案是删除所有&lt;/TextView&gt;并在属性末尾直接将其关闭/&gt;,例如:

<TextView
    android:id="@+id/simplefragment"
    android:layout_width="0px"
    android:layout_weight="2"
    android:orientation="vertical"
    android:padding="@dimen/component_padding"
    android:layout_height="match_parent" />

在所有 TextView 中执行此操作。另外,检查是否有一些&lt;TextView&gt;标签里面有任何其他视图,如果有,那么你应该把它删除并放在其他地方。

【讨论】:

    【解决方案2】:

    我想你可能有 findViewById 那个特定的 textview 并将它转换为一个 ViewGroup 所以例如:

    ViewGroup simpleFragment = (ViewGroup) findViewById(R.id.simplefragment);

    您不能将 textview 转换为 viewGroup,因为它不是 ViewGroup,textview 是 View。

    如果你查看 android 文档,textview extends View: http://developer.android.com/reference/android/widget/TextView.html

    ViewGroups 是像 RelativeLayout 或 LinearLayouts 或 FrameLayouts 这样的对象,其中可以包含多个视图。

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多