此代码显示错误:
private void selectItem(int position) {
Fragment fragment;
switch (position)
{
case 1:
fragment = new PizzaFragment();
break;
case 2:
fragment = new PastaFragment();
break;
case 3:
fragment = new StoresFragment();
break;
default:
fragment = new TopFragment();
}
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame,fragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
当我使用新的PizzaFragment或PastaFragment初始化片段时,android studio会显示错误"不兼容的类型"
答案 0 :(得分:0)
我做了同样的一次,错误是PizzaFragment
和PastatFragent
类没有扩展片段,只看一次
<强>像强>
PizzaFragment extends Fragment{
和
PastaFragment extends Fragment{
也许 您正在导入不同的包,因此请检查您的导入语句
答案 1 :(得分:0)
你必须创建该片段的对象。它会工作。
switch (position)
{
case 1:
PizzaFragment fragment = new PizzaFragment();
// apply FragmentTransaction
break;
case 2:
PastaFragment fragment = new PastaFragment();
// apply FragmentTransaction
break;
case 3:
StoresFragment fragment = new StoresFragment();
// apply FragmentTransaction
break;
default:
TopFragment fragment = new TopFragment();
// apply FragmentTransaction
}