【问题标题】:Android fragment framework: up-casting does not workAndroid片段框架:向上转换不起作用
【发布时间】:2012-11-26 02:29:22
【问题描述】:

我正在处理活动中的 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 扩展了 ListFragment 扩展了 Fragment。这里应该是隐式向上转换,我不明白为什么它不起作用:(

//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;
    }

【问题讨论】:

  • 确保您没有使用支持库的 ListFragment (android.support.v4.app.Fragment),或者如果您使用了,请确保您也在使用支持库中的 Fragment 类。
  • 非常感谢。我没有意识到有两个具有相同类的包。
  • @user1886616 是的,我想这让每个人都明白。 Google 认为默认使用 ADT 17(?)创建的每个新应用程序都自动使用支持库是一个好主意。

标签: java android fragment upcasting


【解决方案1】:
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 rootView;
        if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("1")){
            rootView = inflater.inflate(R.layout.fragment_tab1,container, false);               
        }
        else if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("2")){
            rootView = inflater.inflate(R.layout.fragment_tab2,container, false);               
        }
        else if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("3")){
            rootView = inflater.inflate(R.layout.fragment_tab3,container, false);               
        }
        else{
            rootView = inflater.inflate(R.layout.fragment_tab,container, false);
        }
        return rootView;
    }
}

//确保你使用 import android.support.v4.app.ListFragment -->

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 2017-07-13
    • 1970-01-01
    相关资源
    最近更新 更多