我试图单击打开签名片段的按钮,但出现以下崩溃: java.lang.IllegalArgumentException:找不到片段SignFragment {e65f679#3 id = 0x7f0a03ca}的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>
答案 0 :(得分:1)
您在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;
}
Check out this link for more details and tutorial on fragment.
答案 1 :(得分:0)
使用此代码:-
Fragment fragment = SignFragment.getInstance(this);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction()
fragmentTransaction.add(R.id.orderingMainContainerfragment, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
并导入
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;