简体   繁体   中英

Wrong 2nd argument type: calling .replace() from within fragment

I know this question is asked often but none of the accepted solutions have worked for me. ( reference solution )

I'm getting the classic "Wrong 2nd argument type" error when I try to launch a Preference Menu fragment from within one of my otherfragments.

Unforunately, even after doing what the accepted answer in that question suggests (calling FragmentManager fragmentManager = getSupportFragmentManager() and importing android.support.v4.app.FragmentManager; I still see the "Wrong 2nd argument type" bug when I call .replace().

A quick overview: my MainActivity.java opens a fragment viewpager (MenuPager.java), from which I try to launch another fragment (FragmentSettingsMenu.java) when a button is clicked within FragmentTrackRecordMenu.java (a child fragment of MenuPager.java).

My code is below. I try to launch the settings menu fragment when the menu button (within FragmentTrackRecord) is clicked but I can't get around the aforementioned compiler error. How can I display my PreferencesFragment successfully from this onClick?

MainActivity.java

import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {
    public static ViewPager menuPager;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        menuPager = (ViewPager) findViewById(R.id.pager);
        PagerAdapter pageAdapter = new MenuPagerAdapter(getSupportFragmentManager());
        menuPager.setAdapter(pageAdapter);
        ...
    }
}

MenuPagerAdapter.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MenuPagerAdapter extends FragmentPagerAdapter {

    public MenuPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        switch (arg0) {
            case 0:
                return new FragmentNeckDisplayMenu();
            case 1:
                return new FragmentCapoMenu();
            case 2:
                return new FragmentTrackRecordMenu();
            default:
                break;
        }
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }
}

FragmentTrackRecordMenu.java

    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;

    public class FragmentTrackRecordMenu extends Fragment {
        private Button menuIcon;

    public FragmentTrackRecordMenu(){    }   //default constructor

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.menu_fragment_recorder, container, false);

        menuIcon = (Button) v.findViewById(R.id.menuIcon);
        menuIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //open popup window
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(android.R.id.content, new FragmentSettingsMenu()) //COMPILER ERROR
                        .commit();
            }
        });
    }
}

FragmentSettingsMenu.java

    public class FragmentSettingsMenu extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from the XML resource
        addPreferencesFromResource(R.xml.preferences);
    }
}

getSupportFragmentManager() returns the fragment manager of android.support.v4.app.FragmentManager and your FragmentSettingMenu.java is of android.app.Fragment. That's why "Wrong 2nd argument type" error because they are incompatible.

your FragmentManager is of v4 type and FragmentSettingsMenu is not a v4 type.Hence it does not allow.Try the below line in activity.

 getFragmentManager().beginTransaction().replace(android.R.id.content,
            new PrefsFragment()).commit();

.try this link here

it may give you some help.

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