如何在屏幕上只运行一个片段?

时间:2014-03-25 21:06:28

标签: java android android-fragments

我想从另一个片段运行一个片段。我试试:

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

    View rootView = inflater.inflate(R.layout.lecturers_fragment,
            container, false);
    ListView list = (ListView) rootView.findViewById(R.id.lecturersList);
    final List<Lecturer> allLecturersList = LecturerDatabaseHelper
            .getAllLecturers(getActivity());
    if (allLecturersList != null) {
        LecturerItemAdapter lecturerAdapter = new LecturerItemAdapter(
                mCurrentActivity, allLecturersList);
        list.setAdapter(lecturerAdapter);
    }
    list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view,
                int index, long id) {

            Lecturer lecturer = allLecturersList.get(index);
            L.i("Boulder name  is playing link it contains"
                    + lecturer.getName());

            Intent intent_lecturer = new Intent(mCurrentActivity,
                    LecturerFragment.class);
            intent_lecturer.putExtra(LecturerFragment.SELECTED_LECTURER,
                    lecturer);
            mCurrentActivity.startActivity(intent_lecturer);
        }
    });
    return rootView;
}

在我的logcat中我有这个:

03-25 22:04:36.092: E/AndroidRuntime(12453): Caused by: java.lang.ClassCastException: com.asi.sesjaapp.view.LecturerFragment cannot be cast to android.app.Activity

我该怎么做?

1 个答案:

答案 0 :(得分:0)

  Intent intent_lecturer = new Intent(getActivity(), YourActivity.class);
            intent_lecturer.putExtra(LecturerFragment.SELECTED_LECTURER, lecturer);
            mCurrentActivity.startActivity(intent_lecturer);

你是一个片段,而不是mCurrentActivity只是给你的父活动,如下所示:

new Intent(getActivity() , LecturerFragment.class);

再次看起来LecturerFragment.class是一个应该是活动的片段。

注意:确保您的父活动扩展了FragmentActivity

如果此答案对您没有帮助,请发布您的父活动代码和片段代码。