Android 由于布局转换导致崩溃
Android crash due to layout casting
Android Studio 1.1.0,java 8,Windows 7 专业版 64 位
我的应用程序因 运行 次致命异常而崩溃,日志中出现以下错误。
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams 无法转换为 android.widget.LinearLayout$LayoutParams
如果我注释掉 java 文件中的第二行,它不会崩溃。但是我该如何解决这个问题,我没有在我的代码中看到这样的转换。
谢谢
RelativeLayout headerSection = (RelativeLayout)findViewById(R.id.contact_view_header); //fetch the layout
headerSection.setLayoutParams(new RelativeLayout.LayoutParams(width, (int) (width * (9.0 / 16.0))));
这是 xml 文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="au.com.totalcareauto.sam.mycontacts.ContactViewActivity">
<RelativeLayout
android:id="@+id/contact_view_header"
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:id="@+id/contact_view_image"
android:src="@mipmap/sunset"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/contact_view_name"
android:layout_alignParentBottom="true"
android:layout_marginLeft="@dimen/vertical_margin_small"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@android:color/white"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v7.widget.Toolbar
android:id="@+id/contact_view_toolbar"
android:fitsSystemWindows="true"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
<ListView
android:id="@+id/contact_view_fields"
android:layout_width="match_parent"
android:layout_weight="100"
android:layout_height="0dp" />
您对 LayoutParams 子类的选择有误。
LayoutParams 的正确形式应该取决于 LinearLayout 而不是 RelativeLayout 作为 LinearLayout 是您的 父视图 而 RelativeLayout 是 子视图 .
所以你可以使用 LinearLayout.LayoutParams 而不是 RelativeLayout.LayoutParams 或者你可以使用 RelativeLayout 作为你的根视图.
RelativeLayout headerSection = (RelativeLayout)findViewById(R.id.contact_view_header); //fetch the layout
headerSection.setLayoutParams(new LinearLayout.LayoutParams(width, (int) (width * (9.0 / 16.0))));
我想这会解决你的问题。我已经测试过了,它工作正常。
Android Studio 1.1.0,java 8,Windows 7 专业版 64 位
我的应用程序因 运行 次致命异常而崩溃,日志中出现以下错误。 java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams 无法转换为 android.widget.LinearLayout$LayoutParams
如果我注释掉 java 文件中的第二行,它不会崩溃。但是我该如何解决这个问题,我没有在我的代码中看到这样的转换。
谢谢
RelativeLayout headerSection = (RelativeLayout)findViewById(R.id.contact_view_header); //fetch the layout
headerSection.setLayoutParams(new RelativeLayout.LayoutParams(width, (int) (width * (9.0 / 16.0))));
这是 xml 文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="au.com.totalcareauto.sam.mycontacts.ContactViewActivity">
<RelativeLayout
android:id="@+id/contact_view_header"
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:id="@+id/contact_view_image"
android:src="@mipmap/sunset"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/contact_view_name"
android:layout_alignParentBottom="true"
android:layout_marginLeft="@dimen/vertical_margin_small"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@android:color/white"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v7.widget.Toolbar
android:id="@+id/contact_view_toolbar"
android:fitsSystemWindows="true"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
<ListView
android:id="@+id/contact_view_fields"
android:layout_width="match_parent"
android:layout_weight="100"
android:layout_height="0dp" />
您对 LayoutParams 子类的选择有误。
LayoutParams 的正确形式应该取决于 LinearLayout 而不是 RelativeLayout 作为 LinearLayout 是您的 父视图 而 RelativeLayout 是 子视图 .
所以你可以使用 LinearLayout.LayoutParams 而不是 RelativeLayout.LayoutParams 或者你可以使用 RelativeLayout 作为你的根视图.
RelativeLayout headerSection = (RelativeLayout)findViewById(R.id.contact_view_header); //fetch the layout
headerSection.setLayoutParams(new LinearLayout.LayoutParams(width, (int) (width * (9.0 / 16.0))));
我想这会解决你的问题。我已经测试过了,它工作正常。