I\'m getting the following error: \"The method getSupportFragmentManager() is undefined for the type new View.OnClickListener(){}\" in my fragment
I did a diff on Marco's answer to see what he actually recommended changing: just
d.show(getSupportFragmentManager(), "dialog");
to
FragmentManager fm =
ObstetricsFragment1.this.getSherlockActivity().getSupportFragmentManager();
d.show(fm, "dialog");
You need to extend ObstetricsFragment1 from SherlockDialogFragment.
you will get rid of this problem by making the activity that calls the dialog extend FragmentActivity
public class ObstetricsFragment1 extends FragmentActivity{
this is inherent to the support library, as seen in DIALOG SPECS and SUPPORT LIBRARY SPECS
To get FragmentManager
in a fragment call getChildFragmentManager()
.
See this.
Change this
mPickLMPDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LMPDatePickerDialogFragment d = LMPDatePickerDialogFragment.newInstance();
d.show(getSupportFragmentManager(), "dialog");
}
});
to
mPickLMPDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LMPDatePickerDialogFragment d = LMPDatePickerDialogFragment.newInstance();
d.show(getChildFragmentManager(), "dialog");
}
});
There is a problem in your project because you are using the actionBarSherlock which is deprecated... You should take a look at Android Support and use it because the supportFragmentManager is available with it. It's pretty easy to use it, add it in your build.gradle
compile "com.android.support:support-core-utils:25.2.0"
After just extends your activities and fragment with FragmentActivity or Fragment from support. Maybe you will have to make some change because of using sherlock bar
Another thing about your problem is that you called supportFragmentManager from a fragment... you should called
getChildFragmentManager() // because it will be a nested fragment
Hope it's helpful
In case anyone is using SherlockActivity
instead of SherlockFragment
just extend the activity to SherlockFragmentActivity
.