简体   繁体   English

使用Listfragment替换片段时出错

[英]Error to replace fragment by Listfragment

I did this 我这样做了

@Override
public void onNavigationDrawerItemSelected(int position) {
    Fragment obj = null;
    ListFragment listfragment = null;
    switch (position) {
        case 0:
            listfragment= new F1_fr();
            break;
        case 1:
            obj= new F6_fr();
            break;
        case 2:
            obj= new F3_fr();
            break;
        case 3:
            obj= new F4_fr();
            break;
        case 4:
            obj= new F5_fr();
            break;
    }

    if (obj != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
            .replace(R.id.container, obj)
            .commit();
    }
    else if (listfragment != null) {
        // do stuff if its a listfragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
            .replace(R.id.container, listfragment)
            .commit();
    }
}

But i have an error here : .replace(R.id.container, listfragment) that said me replace android.supportv4.app.fragment in fragment transaction cannot be applied to(int, android.app.ListFragment) 但我在这里有一个错误:.replace(R.id.container,listfragment)说我替换片段事务中的android.supportv4.app.fragment不能应用于(int,android.app.ListFragment)

    public class F1_fr extends ListFragment {
   View rootview;
    TextView t;
    ListView listView;

    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootview=inflater.inflate(R.layout.f1_lay,container,false);
        listView =(ListView) rootview.findViewById(R.id.list);
        rootview.findViewById(R.id.semi_transparent).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getActivity(), qr.class);
                ;

                startActivity(intent);


            }
        });



        return rootview;

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

        final String[] items = getResources().getStringArray(R.array.heroes);
        final ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, items);

        setListAdapter(aa);

    }

}

When you automatically optimize your imports (like Ctrl-Shift-O in Eclipse), the system should give you a choise what Fragment to import. 当您自动优化导入(如Eclipse中的Ctrl-Shift-O)时,系统应该选择要导入的片段。 Choose a support version or a ListFragment like laalto said. 选择支持版本或像Lalto这样的ListFragment说。

You're mixing android.app fragments with support library fragments. 您将android.app片段与支持库片段混合在一起。

Since your intention seems to be using support library fragments, replace your android.app fragment imports such as android.app.ListFragment with their support library counterparts such as android.support.v4.app.ListFragment . 由于您的意图似乎是使用支持库片段,因此将android.app片段导入(例如android.app.ListFragment替换为其支持库对应物(如android.support.v4.app.ListFragment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM