Fragment事务中FrameLayout的IllegalArgumentException

时间:2018-10-29 12:16:20

标签: android

这里是OnClickListener

holder.btnTakeTest.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ((FragmentActivity) mCtx).getSupportFragmentManager().beginTransaction()
            .replace(R.id.container_id, new TestFragment(), "TestFragment")
            .addToBackStack(null)
            .commit();
   }
});

这是堆栈跟踪:

2018-10-29 17:38:24.918 7192-7192/com.vss.app.apprentice E/FragmentManager: 
No view found for id 0x7f08003c (com.vss.app.apprentice:id/container_id) for fragment TestFragment{50671a3 #3 id=0x7f08003c TestFragment}
2018-10-29 17:38:24.918 7192-7192/com.vss.app.apprentice E/FragmentManager: Activity state:
2018-10-29 17:38:24.957 7192-7192/com.vss.app.apprentice E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.vss.app.apprentice, PID: 7192
    java.lang.IllegalArgumentException: No view found for id 0x7f08003c (com.vss.app.apprentice:id/container_id) for fragment TestFragment{50671a3 #3 id=0x7f08003c TestFragment}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6354)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
2018-10-29 17:38:25.459 8442-8442/com.vss.app.apprentice E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so...

片段布局:

<FrameLayout
    android:id="@+id/container_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

列表文件:

 <android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#e5e7e7">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

    <TextView
        android:id="@+id/txt_recipe_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chettinad Fish Fry - Tutorial"
        android:textSize="16dp"
        android:textStyle="bold"
        android:layout_margin="5dp"
        android:textColor="#000"/>

    <TextView
        android:id="@+id/txt_completed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Completed"
        android:textSize="16dp"
        android:layout_margin="5dp"
        android:textStyle="italic"/>

    <TextView
        android:id="@+id/txt_take_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do You Want Take Test?"
        android:textSize="16dp"
        android:layout_margin="5dp" />

            <TextView
                android:id="@+id/txt_congrats"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Congratulations !"
                android:textSize="18dp"
                android:layout_gravity="center"
                android:textStyle="bold"
                android:layout_margin="5dp"
               android:textColor="@color/colorPrimary" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="40dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:layout_margin="5dp"
        android:weightSum="3"
        android:background="#FFF">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">

        <TextView
            android:id="@+id/txt_test_complete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test Completed"
            android:textSize="16dp"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">

        <TextView
            android:id="@+id/txt_score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Your Score 8/10"
            android:layout_gravity="end"
            android:textSize="16dp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:id="@+id/btn_take_test"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Take Test"
                android:textSize="16dp"
                android:textColor="#FFF"
                android:background="@color/colorPrimary"/>
        </LinearLayout>

    </LinearLayout>

    </LinearLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

这是按钮findviewby id:

 public static class RecyclerVH extends RecyclerView.ViewHolder {

    Button btnTakeTest;

    public RecyclerVH(@NonNull View itemView) {
        super(itemView);

        btnTakeTest = itemView.findViewById(R.id.btn_take_test);
    }
}

1 个答案:

答案 0 :(得分:2)

您的布局文件必须缺少Fragment元素,或者其id不是container_id。

func loadInitialState<T>(inital: InitialState) -> T?  {
    switch inital {
    case .listTableView(let test):
        return test as? T
    }
    return nil
}

请检查您的布局文件。