John Ranniel Sinel
John Ranniel Sinel

Reputation: 87

getSupportFragmentManager() is not working on onCreateView() method of the fragment class.

Goodevening. I have read several questions here in stackoverflow and some tutorial articles and site about fragments. I'm currently passing some data on the first fragment class to the second fragment class. But I've got some error on the part of transaction in the bundle, It says in the error that the method getSupportFragmentManager() method is undefined.

 getSupportFragmentManager().beginTransaction()
         .replace(R.id.content_frame, cblf).commit();

What is the bestway to fix this problem? Anyways, here is my whole code,

package com.example.navigationdrawerexample;

import android.app.Fragment;
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.TextView;
import android.widget.Toast;

public class CollegeBulletinFragment extends Fragment implements OnClickListener {

    Button ccs;
    Button coe;
    Button coed;
    Button con;
    Button cba;
    Button cas;
    Button cihm;
    Button gn;
    public CollegeBulletinFragment(){   
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_collegebulletin, container, false);

        gn = (Button) rootView.findViewById(R.id.gn);
        ccs = (Button) rootView.findViewById(R.id.ccs);
        coe = (Button) rootView.findViewById(R.id.coe);
        coed = (Button) rootView.findViewById(R.id.coed);
        con = (Button) rootView.findViewById(R.id.con);
        cba = (Button) rootView.findViewById(R.id.cba);
        cas = (Button) rootView.findViewById(R.id.cas);
        cihm = (Button) rootView.findViewById(R.id.cihm);

        gn.setOnClickListener(this);
        ccs.setOnClickListener(this);
        coe.setOnClickListener(this);
        coed.setOnClickListener(this);
        con.setOnClickListener(this);
        cba.setOnClickListener(this);
        cas.setOnClickListener(this);
        cihm.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View arg0) {
        String passingword = "";
        switch (arg0.getId()){
        case R.id.ccs:
            passingword = "CCS";
            break;
        case R.id.coe:
            passingword = "COE";
            break;
        case R.id.gn:
            passingword = "GN";
            break;
        case R.id.coed:
            passingword = "COED";
            break;
        case R.id.con:
            passingword = "CON";
            break;
        case R.id.cba:
            passingword = "CBA";
            break;
        case R.id.cas:
            passingword = "CAS";
            break;
        case R.id.cihm:
            passingword = "CIHM";
            break;

        }

        CollegeBulletinListFragment cblf = new CollegeBulletinListFragment();
        Bundle args = new Bundle();
        args.putString("passingWord", passingword);
        cblf.setArguments(args);

         getSupportFragmentManager().beginTransaction()
         .replace(R.id.content_frame, cblf).commit();
    }
}

Upvotes: 1

Views: 2447

Answers (4)

Kevin Robatel
Kevin Robatel

Reputation: 8386

You have to use the support library android.support.v4... for using getSupportFragmentManager().

Upvotes: 2

PPartisan
PPartisan

Reputation: 8231

There are two types of Fragment, "regular", for which the import declaration is android.app.Fragment, and "support", which is android.support.v4.app.Fragment. Your Fragment isn't a "support" Fragment, evidenced by the import declaration in your code:

package com.example.navigationdrawerexample;

**import android.app.Fragment;**
import android.os.Bundle;

Second, you access getSupportFragmentManager() from a FragmentActivity (and thus any class that extends FragmentActivity, such as ActionBarActivity and AppCompatActivity)

So, to begin with, you should change your import declaration to android.support.v4.app.Fragment.

Then, you should ensure the Activity that hosts the Fragment is a FragmentActivity (or AppCompatActivity etc.)

Finally, inside your Fragment, you should be able to call getActivity().getSupportFragmentManager().

You can also browse the documentation for Communicating with Other Fragments, as I would personally execute the code in your question from the Activity, not the Fragment itself, via an interface (here's a link to a related question that shows this technique).

Upvotes: 1

Kevin Coppock
Kevin Coppock

Reputation: 134704

getSupportFragmentManager() is defined on AppCompatActivity to avoid clashing with the native (non-support) getFragmentManager(). However, for the support Fragment class, there's no existing method to clash with, so you should instead use:

getFragmentManager()

to get the hosting Activity's FragmentManager, or alternatively, use:

getChildFragmentManager()

to the the Fragment's own FragmentManager (for nesting Fragments)

Upvotes: 2

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

error that the method getSupportFragmentManager() method is undefined

Because getSupportFragmentManager() method is not available in Fragment class it is in FragmentActivity so use getActivity() to access it.like:

getActivity().getSupportFragmentManager().beginTransaction()
         .replace(R.id.content_frame, cblf).commit();

Upvotes: 2

Related Questions