【问题标题】:Android - android.widget.ImageView cannot be cast to android.view.ViewGroupAndroid - android.widget.ImageView 无法转换为 android.view.ViewGroup
【发布时间】:2015-10-28 16:33:52
【问题描述】:

尝试在模拟比萨表单上获取比萨徽标的背景。

由于某种原因,我不断收到错误消息。

这是布局xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_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=".Short_Williams_Assignment3">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/photo"
    android:id="@+id/pizzaLogo"
    android:src="@drawable/pizza">
<LinearLayout>
 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/title_label"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Extra Large"
        android:id="@+id/radioButton4"
        android:checked="false" />

</LinearLayout>
</ImageView>
</FrameLayout>

我不确定为什么我不断收到此错误。我尝试将图像视图包装在框架布局中,但它不起作用。

感谢您提供的任何帮助!

【问题讨论】:

    标签: android imageview android-framelayout


    【解决方案1】:

    问题是&lt;/ImageView&gt;。由于 ImageView 无法托管视图,因此您无法将它们包裹起来。摆脱&lt;/ImageView&gt;并从

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/photo"
        android:id="@+id/pizzaLogo"
        android:src="@drawable/pizza">
    

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/photo"
        android:id="@+id/pizzaLogo"
        android:src="@drawable/pizza" />
    

    /&gt; 将关闭ImageView 的标签

    【讨论】:

      【解决方案2】:

      你不能把任何 View 放在 ImageView Tag 中。

      如果您想设置该徽标,请尝试像这样为您的线性布局使用背景。

      <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/pizza">
          <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:text="@string/title_label"
          android:id="@+id/textView"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true" />
      
          <RadioButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Extra Large"
          android:id="@+id/radioButton4"
          android:checked="false" />
      
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 2012-06-26
        • 1970-01-01
        • 1970-01-01
        • 2017-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        相关资源
        最近更新 更多