2

アクティビティ内のListfragmentに取り組んでいます。アクティビティにネストされたクラスがあります。

public static class DummySectionFragment extends ListFragment {
    /**
     * 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 contentView=inflater.inflate(R.layout.activity_main_fragment_browse, container);
        return contentView;
    }

しかし、コンパイラはDummySectionFragemtがフラグメントではないと文句を言います

    @Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, show the tab contents in the
    // container view.
    Fragment fragment = new DummySectionFragment(); // <-- complain not fragment
    // codes omitted .......
}

コンパイラからの苦情:タイプの不一致:MainActivity.DummySectionFragmentからFragmentに変換できません

DummySectionFragmentがFragmentを直接拡張するようにすると、それは機能しますが、以前は機能しなかった理由がわかりません。どうやら、DummySectionFragmentはFragmentを拡張するListFragmentを拡張します。これはここでは暗黙のアップキャストであるはずですが、なぜ機能しないのかわかりません:(

//make it directly extends 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 contentView=inflater.inflate(R.layout.activity_main_fragment_browse, container);
        return contentView;
    }
4

1 に答える 1