getSupportFragmentManager() is undefined

后端 未结 8 1192
生来不讨喜
生来不讨喜 2020-12-29 00:50

I\'m getting the following error: \"The method getSupportFragmentManager() is undefined for the type new View.OnClickListener(){}\" in my fragment

相关标签:
8条回答
  • 2020-12-29 01:29

    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");
    
    0 讨论(0)
  • 2020-12-29 01:29

    You need to extend ObstetricsFragment1 from SherlockDialogFragment.

    0 讨论(0)
  • 2020-12-29 01:38

    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

    0 讨论(0)
  • 2020-12-29 01:40

    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");
        }
    
    });
    
    0 讨论(0)
  • 2020-12-29 01:43

    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

    0 讨论(0)
  • 2020-12-29 01:44

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

    0 讨论(0)
提交回复
热议问题