簡體   English   中英

將片段替換為片段:Exception找不到ID為0x的視圖

[英]Replace fragment inside fragment :Exception No view found for id 0x

我有一個片段,我嘗試替換布局中的另一個片段。這是一個片段替換代碼

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布局內的內容,但是我有此異常。 我究竟做錯了什么? 謝謝。

要在另一個布局中實現一個Fragment ,您需要在另一個fragment_layout.xml文件中的Fragment中顯示布局。 因此,您需要按如下方式修改當前布局。

<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>

據我所知,您內部的嵌套布局太多了。 您需要根據需求和活動和片段之間的事務流重新布置布局。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM