getSupportFragmentManager() is undefined

。_饼干妹妹 提交于 2019-11-30 04:10:23

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

Try changing your code to this:

public class ObstetricsFragment1 extends SherlockFragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    //Fragment Layout
    View view = inflater.inflate(R.layout.obstetricsfragment1, container, false);

    Button mPickLMPDate = (Button) view.findViewById(R.id.pickLMPDate);

    mPickLMPDate.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            LMPDatePickerDialogFragment d = LMPDatePickerDialogFragment.newInstance();
            FragmentManager fm = ObstetricsFragment1.this.getSherlockActivity().getSupportFragmentManager();
            d.show(fm, "dialog");
        }

    });

    return view;
}

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");

In case anyone is using SherlockActivity instead of SherlockFragment just extend the activity to SherlockFragmentActivity.

I had a similar problem loading a lesson project (https://developer.android.com/training/multiple-threads).

To resolve, I had to add an external jar (sdk/extras/android/support/v4/android-support-v4.jar).

Hope this helps.

You need to extend ObstetricsFragment1 from SherlockDialogFragment.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!