对LayoutParams的意外隐式强制转换

问题描述 投票:1回答:2

这可能是非常简单的事情,但我是一个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>

感谢任何帮助或建议。谢谢

android android-linearlayout
2个回答
0
投票

对不起,我读错了。

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

问题是这个

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

这里android试图获取一个id为“frame_layout”的片段

但是这个id属于FrameLayout而不是片段。

您应该使用以下标识声明标记:

<fragment>

0
投票

你需要添加另一个括号

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

例如,

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

© www.soinside.com 2019 - 2024. All rights reserved.