替换片段内的片段:异常没有找到id 0x的视图

时间:2018-02-22 07:10:50

标签: java android android-fragments

我有一个片段,我尝试在我的布局中替换另一个片段。这是一个片段替换代码

public static void replace(FragmentManager fm, Fragment fragment, int container, String tag) {
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(container, fragment, tag);
    fragmentTransaction.commit();
}

这是一个主要代码

if (cardList != null && cardList.isEmpty()) {
    hideDialog(pDialog);
    drawavleView.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    norecordfragment = new NoRecordFragment();
    norecordfragment.setAvatar(R.drawable.ic_card_nocard);
    norecordfragment.setText(getActivity().getString(R.string.u_cards_no_data_text));
    norecordfragment.setButtontext(getActivity().getString(R.string.u_cards_no_data_action));
    norecordfragment.setAllowToShowAddAction(false);
    floatingActionButton.setVisibility(View.VISIBLE);
    FragmentUtils.replace(getActivity().getSupportFragmentManager(), norecordfragment,
            R.id.upload_main_layout_upload,"norecordfragment");
}

这是我的xml代码

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:fitsSystemWindows="false">


    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:minHeight="@dimen/u_base_min_height">

        <RelativeLayout
            android:id="@+id/upload_main_layout_upload"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff">

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/lay_v3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/white"
                android:orientation="vertical">

                <RelativeLayout
                    android:id="@+id/upload_header_layout"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/transfer_headerView_height"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:background="#f2f2f2" />

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/u_common_margin_left"
                    android:layout_marginRight="@dimen/u_common_margin_left"
                    android:layout_marginTop="@dimen/withdrow_change_card_height"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/u_widget_height"
                        android:background="@drawable/edittext_input_background_not_focus"
                        android:orientation="horizontal">

                        <EditText
                            android:id="@+id/u_major_text"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_marginBottom="7dp"
                            android:layout_marginLeft="5dp"
                            android:layout_weight="0.65"
                            android:background="@null"
                            android:gravity="left"
                            android:hint="0.00"
                            android:imeOptions="actionDone"
                            android:inputType="numberDecimal"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:textColor="#4d4d4d"
                            android:textColorHint="#b2b2b2"
                            android:textSize="35dp" />

                        <Spinner
                            android:id="@+id/upload_spinner"
                            style="@style/Widget.MyApp.HeaderBar.Spinner"
                            android:layout_width="80dp"
                            android:layout_height="match_parent"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:background="@drawable/edittext_input_background_not_focus" />

                        <ImageView
                            android:id="@+id/u_open_spinner"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignBottom="@+id/transfer_user_description"
                            android:layout_alignParentEnd="true"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:layout_gravity="center"
                            android:padding="@dimen/slide_menu_hide_image"
                            android:src="@drawable/ic_menu_downarrow_grey" />
                    </LinearLayout>

                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/lay_v4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:orientation="vertical"
                android:paddingLeft="@dimen/u_common_margin_left"
                android:paddingRight="@dimen/u_common_margin_left">

                <com.ripperLayout.MaterialRippleLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:layout_width="fill_parent"
                    android:layout_height="@dimen/u_widget_height"
                    android:descendantFocusability="blocksDescendants"
                    android:gravity="center_vertical"
                    app:mrl_rippleAlpha="0.2"
                    app:mrl_rippleColor="#ffffff"
                    app:mrl_rippleDelayClick="false"
                    app:mrl_rippleHover="true"
                    app:mrl_rippleOverlay="true"
                    app:mrl_rippleRoundedCorners="4dp">

                    <Button
                        android:id="@+id/u_done"
                        android:layout_width="fill_parent"
                        android:layout_height="@dimen/u_widget_height"
                        android:background="@drawable/rounded_corners_blue"
                        android:text="@string/u_register_next"
                        android:textColor="#ffffff"
                        android:textSize="@dimen/u_common_text_size_small" />
                </com.ripperLayout.MaterialRippleLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/u_common_margin_left"
                    android:visibility="invisible" />
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
        <LinearLayout
            android:id="@+id/floating_action_bar_transparent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#4D000000"
            android:clickable="true"
            android:orientation="vertical"
            android:visibility="gone" />

    <android.support.design.widget.FloatingActionButton xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/floating_action_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add_white"
        app:backgroundTint="#48c9ff" />

</android.support.design.widget.CoordinatorLayout>

我不知道为什么在某些设备中我有这个例外。

Exception java.lang.IllegalArgumentException: No view found for id 0x7f09052f (com.me.myapp:id/upload_main_layout_upload) for fragment NoRecordFragment{5309279 #4 id=0x7f09052f norecordfragment}

正如您所看到的,我尝试在upload_main_layout_upload布局中替换我的自定义片段,但我有这个例外。我究竟做错了什么?感谢。

1 个答案:

答案 0 :(得分:0)

要在另一个布局中实现Fragment,您需要在另一个Fragment文件中的fragment_layout.xml内显示您所在的布局。因此,您需要修改当前布局,如下所示。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:fitsSystemWindows="false">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:minHeight="@dimen/u_base_min_height">

        <RelativeLayout
            android:id="@+id/upload_main_layout_upload"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"/>

    </ScrollView>
        <LinearLayout
            android:id="@+id/floating_action_bar_transparent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#4D000000"
            android:clickable="true"
            android:orientation="vertical"
            android:visibility="gone" />

    <android.support.design.widget.FloatingActionButton xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/floating_action_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add_white"
        app:backgroundTint="#48c9ff" />
</android.support.design.widget.CoordinatorLayout>

将内容放在此布局之外的RelativeLayout内,并创建另一个布局,该布局将在Fragment中用于从中获取视图。

这是一个示例fragment_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lay_v3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/upload_header_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/transfer_headerView_height"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="#f2f2f2" />


    <LinearLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/u_common_margin_left"
        android:layout_marginRight="@dimen/u_common_margin_left"
        android:layout_marginTop="@dimen/withdrow_change_card_height"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/u_widget_height"
            android:background="@drawable/edittext_input_background_not_focus"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/u_major_text"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="7dp"
                android:layout_marginLeft="5dp"
                android:layout_weight="0.65"
                android:background="@null"
                android:gravity="left"
                android:hint="0.00"
                android:imeOptions="actionDone"
                android:inputType="numberDecimal"
                android:maxLines="1"
                android:singleLine="true"
                android:textColor="#4d4d4d"
                android:textColorHint="#b2b2b2"
                android:textSize="35dp" />

            <Spinner
                android:id="@+id/upload_spinner"
                style="@style/Widget.MyApp.HeaderBar.Spinner"
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:background="@drawable/edittext_input_background_not_focus" />

            <ImageView
                android:id="@+id/u_open_spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/transfer_user_description"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_gravity="center"
                android:padding="@dimen/slide_menu_hide_image"
                android:src="@drawable/ic_menu_downarrow_grey" />
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

据我所见,你有一个嵌套布局太多了。您需要根据活动和碎片之间的需求和事务流重新排列布局。