0

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);
    }
}
Community
  • 1
  • 1
Cody
  • 1,801
  • 3
  • 28
  • 53
  • your FragmentManager is of v4 type and FragmentSettingsMenu is not a v4 type.Hence it does not allow. – Rushi Ayyappa Nov 30 '16 at 05:31
  • @RushiAyyappa is there a way to may FragmentSettingsMenu use the v4 library? I took that code from a google tutorial so i'm not familiar with how to make the change. – Cody Nov 30 '16 at 14:15

2 Answers2

0

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.

29yash
  • 9
  • 3
  • so what's the fix? Do I change FragmentSettingsMenu somehow? – Cody Nov 30 '16 at 14:14
  • Either use getFragmentManager() in place of getSupportFragmentManager() Or extend FragmentSettingsMenu with android.support.v4.app.Fragment – 29yash Dec 01 '16 at 06:58
0

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.

Rushi Ayyappa
  • 2,708
  • 2
  • 16
  • 32