首页 > 解决方案 > java.lang.IllegalArgumentException:没有为片段 SignFragment 的 id 找到视图

问题描述

我正在尝试单击打开 signfragment 的按钮,但出现下面提到的崩溃:java.lang.IllegalArgumentException: No view found for id 0x7f0a03ca for fragment SignFragment{e65f679 #3 id=0x7f0a03ca}

这是我的活动课

   public class MainActivity extends AppcompatActivity implements SignFragment.SignPadCallback{
       setContentView(R.layout.activity_ordering_main);
        //on click of a button i called this method
        private void openSignPad() {
               Fragment fragment = SignFragment.getInstance(this);
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.add(R.id.orderingMainContainerfragment, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }

        }

片段类

 import android.app.Fragment;
    public class SignFragment extends Fragment  implements View.OnClickListener {


        OrderingMainCallback orderingMainCallback;
        private SignPadCallback callBack;
        private LinearLayout signPadLayout;
        private DrawView drawView;
        private int padWidth;
        private int padHeight;


        public static Fragment getInstance(SignPadCallback callBack) {
            SignFragment fragment = new SignFragment();
            fragment.callBack = callBack;
            return fragment;
        }
    }

布局文件

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/linearLayout5"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@+id/linearLayout6"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0">

                <fragment
                    android:id="@+id/orderingMainContainerfragment"
                    android:name="com.android.emobilepos.ordering.newui.OrderingMainCatalogFragment"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    tools:layout="@layout/fragment_ordering_main_catalog" />

    </LinearLayout>
      <fragment
                    android:id="@+id/orderDetailsfragment3"
                    android:name="com.android.emobilepos.ordering.newui.OrderTotalDetailsFragment"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    tools:layout="@layout/fragment_order_total_details" />
    </LinearLayout>

标签: androidandroid-fragments

解决方案


您在 SignFragment 类中缺少 onCreateView ..

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.sign_fragment /* this will be the layout file for your fragment. */, container, false);

    return view;
}

查看此链接以获取有关片段的更多详细信息和教程。


推荐阅读