【发布时间】:2017-12-29 14:30:56
【问题描述】:
这可能很简单,但我是 Android 新手。代码调试得很好,但是当我尝试生成一个签名的 APK 时,我得到了构建错误。使用 Android Studio 3.0.1
错误:意外隐式转换为 LayoutParams:布局标记为 FrameLayout [WrongViewCast]
该位是下面代码中错误高亮3次的地方(这不是完整的功能,只是错误高亮的位):
findViewById(R.id.frame_layout_2).getLayoutParams()).weight > 0)
public void onBackPressed() {
switch (mCurrentFragmentIndex) {
case ALERTS_FRAGMENT_ID:
if (getScreenWidth() >= dualFragmentWidth &&
((LinearLayout.LayoutParams) findViewById(R.id.frame_layout_2).getLayoutParams()).weight > 0) {
shrinkSecondFragment();
//unselect the list item
if (getSupportFragmentManager().findFragmentById(R.id.frame_layout) != null) {
((CollatedAlertsFragment) getSupportFragmentManager().findFragmentById(R.id.frame_layout)).unselectItem();
}
return;
}
break;
case MYSCHOOP_FRAGMENT_ID:
if (getScreenWidth() >= dualFragmentWidth &&
((LinearLayout.LayoutParams) findViewById(R.id.frame_layout_2).getLayoutParams()).weight > 0) {
shrinkSecondFragment();
return;
}
break;
case SETTINGS_FRAGMENT_ID:
if (getScreenWidth() >= dualFragmentWidth &&
((LinearLayout.LayoutParams) findViewById(R.id.frame_layout_2).getLayoutParams()).weight > 0) {
shrinkSecondFragment();
return;
}
break;
这是 frame_layout_2
的 XML <LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:baselineAligned="true">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/frame_layout_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0" />
</LinearLayout>
感谢任何帮助或建议。谢谢
【问题讨论】:
-
frame_layout_2是 FrameLayout,而不是 LinearLayout。 -
@Code-Apprentice:没关系。 LayoutParams 由作为 LinearLayout 的父级给出。 LayoutParams 将是一个
LinearLayout.LayoutParams实例。 -
@DeeV 我建议你验证这个假设
标签: android android-linearlayout