无法转换为片段listfragment

时间:2016-04-03 12:05:21

标签: android android-fragments

我有MainActivity,即导航栏,有不同的片段

public boolean onNavigationItemSelected(MenuItem item) {

    // Handle navigation view item clicks here.
    int id = item.getItemId();;
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction();

    if (id == R.id.nav_camera) {
       fragmentManager.beginTransaction().replace(R.id.container,fcafee).commit();
    } else if (id == R.id.nav_gallery) {
        fragmentManager.beginTransaction().replace(R.id.container.fclubs).commit();
    } else if (id == R.id.nav_slideshow) { //erorr here
        fragmentManager.beginTransaction().replace(R.id.container,feat).commit();
    } else if (id == R.id.nav_manage) {
        fragmentManager.beginTransaction().replace(R.id.container,fsnack).commit();
    } else if (id == R.id.nav_share) {
        fragmentManager.beginTransaction().replace(R.id.container,fsetting).commit();
    }

在我的Fragment fclubs中,我有ListView

public class FragmentClubs extends ListFragment {

我检查了所有的进口,但似乎都没问。

但是我在ListFragment

的行中收到错误消息
Error:(108, 71) error: incompatible types: FragmentClubs cannot be converted to Fragment

Full Fclubs:

public class FragmentClubs extends ListFragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

ListView listView;
String[] names;
final String LOG_TAG = "myLogs";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public FragmentClubs() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment FragmentClubs.
 */
// TODO: Rename and change types and number of parameters
public static FragmentClubs newInstance(String param1, String param2) {
    FragmentClubs fragment = new FragmentClubs();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position,
                                              long id, boolean checked) {
            // Here you can do something when items are selected/de-selected,
            // such as update the title in the CAB
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            // Respond to clicks on the actions in the CAB
            switch (item.getItemId()) {
                case R.id.listView:
                    mode.finish(); // Action picked, so close the CAB
                    return true;
                default:
                    return false;
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate the menu for the CAB
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.eatmenu, menu);
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // Here you can make any necessary updates to the activity when
            // the CAB is removed. By default, selected items are deselected/unchecked.
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // Here you can perform updates to the CAB due to
            // an invalidate() request
            return false;
        }
    });



    return inflater.inflate(R.layout.fragment_clubs,container,false);  // TODO: Rename method, update argument and hook method into UI event
}
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}
  @Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

错误: Here what I get

UPDATED

1 个答案:

答案 0 :(得分:2)

错误可能是因为您拥有的ListFragment来自本地android.app.ListFragment库,而getSupportFragmentManager需要来自android.support.v4.app.ListFragment库的片段。

因此,在您的FragmentClubs课程中,删除import android.app.ListFragment;,并将其替换为import android.support.v4.app.ListFragment

更新:

您需要将onCreateView中的所有代码移至以下单独的方法:

@Override
public void onActivityCreated (Bundle savedInstanceState) {

    // your code
}

这是因为onCreateView仅负责返回视图,其他任何操作都应该在onActivityCreated

中完成