简体   繁体   中英

Open MultipleFragment from main activity

I use class MultipleFragment extends Fragment. I want to open this Fragment from MainActivity. To do so, I use instance of MultipleFragment myfragment :

MultipleFragment myfragment = new MultipleFragment();
            myfragmentTransaction.replace(R.id.fragment_multiple_list_view , myfragment).commit();

And I get an error that argument "myfragment" should be of type Fragment.

error: incompatible types: MultipleFragment cannot be converted to Fragment myfragmentTransaction.replace(R.id.fragment_multiple_list_view , myfragment).commit();

But MultipleFragment extends Fragment... What is right thing to do? How to make the function to recognize the type of extended class in its argument?

public class MainActivity extends AppCompatActivity {

    private Button btnFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnFragment = (Button)findViewById(R.id.btnFragment);
        btnFragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                FragmentManager myfragmentManager = getFragmentManager ();
                FragmentTransaction myfragmentTransaction = myfragmentManager.beginTransaction ();
                MultipleFragment myfragment = new MultipleFragment();
                myfragmentTransaction.replace(R.id.fragment_multiple_list_view , myfragment).commit();

            }
        });
    }
}

public class MultipleFragment extends Fragment implements  AdapterView.OnItemClickListener {

    MultipleAdapter mAdapter;

    public MultipleFragment() {
        // Required empty public constructor
    }

    public static MultipleFragment newInstance() {
        return new MultipleFragment();
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_multiple, container, false);
        ListView listView = (ListView) view.findViewById(R.id.fragment_multiple_list_view);

        MultipleData android = new MultipleData("Android", false);
        MultipleData iPhone = new MultipleData("iPhone", false);
        MultipleData windowsMobile = new MultipleData("WindowsMobile", false);

        MultipleData blackberry = new MultipleData("Blackberry", false);
        MultipleData webOS = new MultipleData("WebOS", false);
        MultipleData ubuntu = new MultipleData("Ubuntu", false);

        MultipleData windows7 = new MultipleData("Windows7", false);
        MultipleData max = new MultipleData("Max OS X", false);
        MultipleData linux = new MultipleData("Linux", false);

        MultipleData os = new MultipleData("OS/2", false);
        MultipleData symbian = new MultipleData("Symbian", false);

        List<MultipleData> list = new ArrayList<>();
        list.add(0, android);
        list.add(1, iPhone);
        list.add(2, windowsMobile);

        list.add(3, blackberry);
        list.add(4, webOS);
        list.add(5, ubuntu);

        list.add(6, windows7);
        list.add(7, max);
        list.add(8, linux);

        list.add(9, os);
        list.add(10, symbian);

        mAdapter = new MultipleAdapter(getActivity(),list);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        mAdapter.setSelectedIndex(position);
        mAdapter.notifyDataSetChanged();
    }
}

You should be using getSupportFragmentManager() , not getFragmentManager() .

FragmentManager myfragmentManager = getSupportFragmentManager()

The framework Fragments ( android.app.Fragment , android.app.FragmentManager ) are deprecated and you should be using the Support Library / AndroidX versions instead.

check if you're mixing android.support.v4.app.Fragment and android.app.Fragment. You need to convert all uses to use the support library, which also means calling getSupportFragmentManager().

For your example:

 android.support.v4.app.FragmentManager myfragmentManager = getSupportFragmentManager();

android.support.v4.app.FragmentTransaction myfragmentTransaction = myfragmentManager.beginTransaction();

 MultipleFragment myfragment = new MultipleFragment();

myfragmentTransaction.replace(R.id.fragment_multiple_list_view, myfragment);

myfragmentTransaction.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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