0

メソッドを使用しようとしています

    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new MappingPage();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

MappingPage() というクラスを作成しました。上記のメソッドを使用しようとすると、作成するフラグメントは拡張されているため MappingPage 型になり、関数がフラグメント (MappingPage オブジェクトではない) を返すためエラーがスローされます。

編集:実行するとエラーが発生します

  Fragment fragment = new MappingPage();

Eclipse は、フラグメントをタイプ MappingPage に変更する必要があることを教えてくれます。つまり、関数の戻り値のタイプを変更する必要があります。

2 つの質問 1) カスタム フラグメントをこれにどのように配置しますか?

2) dummySectionFrament が DummySectionFragment オブジェクトではなく Fragment オブジェクトを返すのはなぜですか?

前もって感謝します

public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy,
                container, false);
        TextView dummyTextView = (TextView) rootView
                .findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return rootView;
    }
}

ここに私のクラスがあります

public class MappingPage  extends Fragment
{
 private MapView map;
 private MapController myMapController;
 public static final String ARG_SECTION_NUMBER = "1";

 public MappingPage() {
 }

public  View onCreateView (LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Context context = new ContextThemeWrapper(getActivity(), R.style.fragment_theme);
    //LayoutInflater Inflater = inflater.cloneInContext(context);

    View view=inflater.inflate(R.layout.fragment_main_dummy, container, false);
    return view;

}

@Override
public void onResume() {
    map = (MapView)getActivity().findViewById(R.id.openmapview);
    map.setBuiltInZoomControls(true);
    myMapController = map.getController();
    myMapController.setZoom(15);
    super.onResume();
}




}
4

1 に答える 1

0

最初の質問に関しては、これでうまくいくはずです。MappingPageクラスの間接インスタンスであるFragmentため、それを返すことができます。

Fragment質問 2 については、呼び出したメソッドは、 の場合のように、常に特定の型を返しますgetItem()。ただし、これはそのサブクラスでもある可能性があり、そうであることが確実な場合は、そのサブクラスに安全にキャストできます。

于 2013-10-27T21:02:16.937 に答える