I am using Android Studio which implements all the functions of a fragment class. I am getting following runtime exception:
must implement OnFragmentInteractionListener
Here is my code and I have implemented the OnFragmentInteractionListener
in my main activity.
MAIN ACTIVITY:
public class MainActivity extends FragmentActivity implements BlankFragment.OnFragmentInteractionListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_container) != null) {
BlankFragment fragment = new BlankFragment();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = manager.beginTransaction();
ft.add(R.id.fragment_container, fragment).commit();
}
@Override
public void onFragmentInteraction(Uri uri) {}
}
BLANK FRAGMENT:
public class BlankFragment extends android.support.v4.app.Fragment {
private OnFragmentInteractionListener mListener;
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(Uri uri);
}
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
}
The remaining methods of fragment class and main activity are also implemented by default. I have also changed LinearLayout
with FrameLayout
in the main_activity.xmlfile and also assigned
android:id` to it which is referenced fine. What is wrong with my code?
To interact with your BlankFragment
object, I would use the following method recommended in the Android Support Docs - I believe this is what you are trying to achieve, and it will be suitable as your BlankFragment
object is hosted by MainActivity
:
public class MainActivity extends FragmentActivity implements BlankFragment.OnFragmentInteractionListener {
private BlankFragment fragment = null;
private android.support.v4.app.FragmentManager manager = null;
private android.support.v4.app.FragmentTransaction ft;
public void onFragmentInteraction(Uri uri){
Log.i("Tag", "onFragmentInteraction called");
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (manager == null) manager = getSupportFragmentManager();
if(manager.findFragmentById(R.id.fragment_container) == null) {
//If a fragment is not already loaded into your container, then add one...
fragment = new BlankFragment();
ft= manager.beginTransaction();
ft.add(R.id.fragment_container,fragment).commit();
}
}
In order to communicate with your fragment, you would do the following:
if(fragment == null) {
fragment = (BlankFragment) manager.findFragmentById(R.id.fragment_container);
}
You can then call any methods associated with fragment
If it's the other way round (communication from fragment to activity), then you would need to do the following in BlankFragment
to form a link with the parent Activity
:
//Class variable...
OnFragmentInteractionListener mCallback;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
You may have forgotten about this last step, which could explain your error. You would then use:
mCallback.onFragmentInteraction(uri);
To communicate with MainActivity
This worked for me, I have just shared it here you never know it will help out some one else.
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
Just one line of code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With