getSupportFragmentManager() is undefined
Asked Answered
I

9

54

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

I have the compatibility library referenced through ABS and the proper imports in place. I reinstalled ABS library w/ the compatibility library, cleaned the project, restarted Eclipse, but nothing has worked.

Essentially, I'm trying to get the fragment to show a date picker through a dialog fragment. Once the date is picked, it must be returned to the fragment so it can be used to calculate information based on that date.

Here's the code for my Fragment:

package com.owentech.abstabsviewpager;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import com.actionbarsherlock.app.SherlockFragment;

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();
            d.show(getSupportFragmentManager(), "dialog");
        }

    });

    return view;
}

public Dialog onCreateDialog(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return null;
}

}

Here's the code for the Dialog Fragment:

package com.owentech.abstabsviewpager;

import android.app.Dialog;
import android.os.Bundle;
import android.widget.DatePicker;
import android.app.DatePickerDialog;

public class LMPDatePickerDialogFragment extends ObstetricsFragment1 implements DatePickerDialog.OnDateSetListener {

static LMPDatePickerDialogFragment newInstance() {
    LMPDatePickerDialogFragment d = new LMPDatePickerDialogFragment();
    return d;
}

private int mLMPYear;
private int mLMPMonth;
private int mLMPDay;

public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new DatePickerDialog(getActivity(), this, mLMPYear, mLMPMonth, mLMPDay);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
    mLMPYear = year;
    mLMPMonth = month;
    mLMPDay = day;
}
}

Finally, here's the code for the my activity:

package com.owentech.abstabsviewpager;

import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.ActionBar.Tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;


public class Obstetrics extends SherlockFragmentActivity
{
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
TextView tabCenter;
TextView tabText;

// START Action Bar Menu Items  
@Override
public boolean onCreateOptionsMenu(Menu menu) {
   MenuInflater inflater = getSupportMenuInflater();
   inflater.inflate(R.menu.main_menu, menu);
   return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
    switch (item.getItemId()){
    case R.id.menuLog:
        ChangeLog cl = new ChangeLog(this);
        cl.getFullLogDialog().show();
        return true;
    case R.id.menuEmail:
        Intent emailIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:[email protected]"));
        startActivity(emailIntent);
        return true;
    case R.id.menuRate:
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=appinventor.ai_shawn_m_gee.MedicalDoctor"));
        startActivity(browserIntent);
        return true;
    case android.R.id.home:
        // App icon in action bar clicked; go home
        Intent intent = new Intent(this, Home.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        return true;
    case R.id.menuExit:
        this.finish();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}      
//END Action Bar Menu Items

// START Tabs View Pager (Add tabs by adding mTabsAdapter.addTab)   
@Override
public void onCreate(Bundle savedInstanceState)
{

    // Information you want returned to your application, via onCreate(), if the activity is destroyed and restarted due to some implicit reason        
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);

    setContentView(mViewPager);
    ActionBar bar = getSupportActionBar();
    bar.setDisplayHomeAsUpEnabled(true);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mTabsAdapter = new TabsAdapter(this, mViewPager);

    mTabsAdapter.addTab(
            bar.newTab().setText("Wheel"),
            ObstetricsFragment1.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("Physical"),
            HistoryPhysicalFragment2.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("ROS"),
            HistoryPhysicalFragment3.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("CAGE"),
            HistoryPhysicalFragment4.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("SIGECAPS"),
            HistoryPhysicalFragment5.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("Glasgow"),
            HistoryPhysicalFragment6.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("Neuro"),
            HistoryPhysicalFragment7.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("Dermat"),
            HistoryPhysicalFragment8.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("Minicog"),
            HistoryPhysicalFragment9.class, null);
}

public static class TabsAdapter extends FragmentPagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener
{
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo
    {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args)
        {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager)
    {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args)
    {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount()
    {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position)
    {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
    }

    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels)
    {
    }

    public void onPageSelected(int position)
    {
        mActionBar.setSelectedNavigationItem(position);
    }

    public void onPageScrollStateChanged(int state)
    {
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft)
    {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++)
        {
            if (mTabs.get(i) == tag)
            {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft)
    {
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft)
    {
    }
}
// END Tabs View Pager

}
Irvingirwin answered 3/11, 2012 at 18:50 Comment(4)
@tony gil You can use AppCompatActivity instead.Rianna
there's a bounty out there, please propose it as an answer and maybe you'll get rewarded. :)Christoper
@tony gil Did you got your answer?Rianna
nothing new was postedChristoper
C
84

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

Christoper answered 18/1, 2013 at 17:1 Comment(3)
This is a frustrating error to have, as adding the correct imports to your class doesn't help.Marijo
very frustrating. "a foot in the bag", as we say in portuguese.Christoper
Unable to extend FragmentActivity, must extend Fragment because I'm using a Navigation Drawer fragment as my base. Anyone know a workaround?Koontz
S
9

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;
}
Scarfskin answered 6/11, 2012 at 6:47 Comment(0)
S
4

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");
Sika answered 4/5, 2013 at 6:26 Comment(0)
M
3

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

Milurd answered 10/4, 2014 at 13:7 Comment(0)
T
2

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.

Township answered 11/10, 2013 at 18:19 Comment(0)
R
1

You need to extend ObstetricsFragment1 from SherlockDialogFragment.

Rufe answered 15/5, 2013 at 22:5 Comment(0)
N
0

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

});
Nagy answered 3/3, 2017 at 8:13 Comment(2)
please expound. some sample code might be nice, too. tks!Christoper
FragmentActivity.getSupportFragmentManager() = android.support.v4.app.Fragment.getFragmentManager() in this case. You don't need nested fragments for this.Rhachis
A
0

You can use AppCompatActivity instead of Activity.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   }
}

Just use AppCompatActivity instead of Activity and then you can able to use getSupportFragmentManager() instead of getFragmentManager().

Athal answered 25/6, 2022 at 15:47 Comment(0)
A
-1

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

Alatea answered 7/3, 2017 at 10:44 Comment(3)
"Migrate from sherlock to appcompat" should be a comment not an answer.Rhachis
Sure but it's relevant to tell it, moreover, My answer focus on the facts to use FragmentActivity and getChildFragmentManager()Alatea
Yes but OP has to be using FragmentActivity already to use SherlockFragment in the first place. What you missed is Fragment.getFragmentManager(). You don't need to introduce another variable by using a child fragment manager and nested fragments. Native and support fragments have access to only their type of manager. That's why it's not named getSupportFragmentManager.Rhachis

© 2022 - 2024 — McMap. All rights reserved.