【问题标题】:Unexpected implicit cast to LayoutParams意外隐式转换为 LayoutParams
【发布时间】: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


【解决方案1】:

对不起,我读错了异常。

这是一个隐式转换错误,而不是显式转换错误。

问题是这样的

getSupportFragmentManager().findFragmentById(R.id.frame_layout)

这里 android 正在尝试获取 id 为“frame_layout”的片段

但是这个id属于FrameLayout而不是fragment。

你应该声明标签:

<fragment>

【讨论】:

  • 您所做的是将视图投射到 params 对象。
  • @DeeV 是的,它甚至无法编译,因为它们完全是不相关的类
  • 你能澄清我做错了什么吗?下划线的错误是这部分代码:findViewById(R.id.frame_layout_2).getLayoutParams()
【解决方案2】:

你需要在周围添加另一个括号

findViewById(R.id.frame_layout_2).getLayoutParams()

例如,

(LinearLayout.LayoutParams) (findViewById(R.id.frame_layout_2).getLayoutParams()).weight

【讨论】:

  • 我试过没有,但没有区别。错误没有显示缺少括号,它说:错误:(678、17)错误:意外隐式转换为LayoutParams:布局标签是FrameLayout [ WrongViewCast]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 2016-07-26
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多