0

当创建一个带有全息片段和选项卡和sectionPagerAdapter 的模板项目时,你会得到这样的东西

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

DummySectionFragment 的定义:

/**
 * A dummy fragment representing a section of the app, but that simply displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    public DummySectionFragment() {
    }

    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER);
        Bundle args = getArguments();
        textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
        return textView;
    }
}

现在我用我自己的片段扩展替换这段代码:

@Override
    public Fragment getItem(int i) {
        Fragment fragment = new TargetsFragment();
        Bundle args = new Bundle();

我在它自己的文件中定义了这个:

public class TargetsFragment extends Fragment {
boolean mDualPane;
int mCurCheckPosition = 0;


private CheckedExpandableListAdapter expandableListAdapter;
private ExpandableListView expandableListView;

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

  //TODO: serve with correct data instead of this!
        final ArrayList<ListNode> dummyList = buildDummyData();
        loadHosts(dummyList);
    }
    [...]

我得到的错误是无法在 getItem 中将 TargetsFragment 转换为 Fragment。我觉得我在这里遗漏了一些简单的东西。但是什么?如果您需要更多代码,请询问。

4

1 回答 1

3

解决方案:仔细查看导入。确保在两个类中导入和扩展相同的 Fragment。例如 android.app.fragment 或导入 android.support.v4.app.Fragment;

于 2013-05-16T20:11:28.493 回答