It's trowing an error saying I must implment the OnFragmentInteractionListener, but as far as I'm aware I'm implementing it properly ... Any help would be greatly appreciated.
Main Activity
import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; public class MainActivity extends Activity implements NavFragment.OnFragmentInteractionListener, ContentFragment.OnFragmentInteractionListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.nav, new NavFragment()); fragmentTransaction.commit(); fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.content, new ContentFragment()); fragmentTransaction.commit(); } @Override public void onNavFragmentInteraction(String string) { Bundle args = new Bundle(); args.putString("string", string); ContentFragment content = new ContentFragment(); content.setArguments(args); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } @Override public void onContentFragmentInteraction(String string){ } }
NavFragment
import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; public class NavFragment extends Fragment implements View.OnClickListener { private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; private String mParam1; private String mParam2; private Button button; private OnFragmentInteractionListener mListener; public static NavFragment newInstance(String param1, String param2) { NavFragment fragment = new NavFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } public NavFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_nav, container, false); button = (Button) v.findViewById(R.id.submit); button.setOnClickListener(this); return v; } public void onButtonPressed(String string) { } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnFragmentInteractionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } @Override public void onClick(View v) { String string = ((EditText) v.findViewById(R.id.gridSize)).toString(); mListener.onNavFragmentInteraction(string); } public interface OnFragmentInteractionListener { public void onNavFragmentInteraction(String string); } }
ContentFragment
import android.app.Activity; import android.app.Fragment; import android.net.Uri; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.GridView; public class ContentFragment extends Fragment { private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public static ContentFragment newInstance(String param1, String param2) { ContentFragment fragment = new ContentFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } public ContentFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_content_fragmet, container, false); String string = "1"; if (getArguments() != null) { Bundle args = getArguments(); string = (String) args.get("string"); } GridView gridView = (GridView) v.findViewById(R.id.gridview); gridView.setNumColumns(Integer.parseInt(string)); gridView.setAdapter(new GridAdapter(this, Integer.parseInt(string))); return v; } public void onButtonPressed(Uri uri) { } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnFragmentInteractionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } public interface OnFragmentInteractionListener { public void onContentFragmentInteraction(String string); } }
error
03-02 18:46:47.254 1958-1958/com.peterchappy.lab5 E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.peterchappy.lab5, PID: 1958 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.peterchappy.lab5/com.peterchappy.lab5.MainActivity}: java.lang.ClassCastException: com.peterchappy.lab5.MainActivity@2298f3ca must implement OnFragmentInteractionListener at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.ClassCastException: com.peterchappy.lab5.MainActivity@2298f3ca must implement OnFragmentInteractionListener at com.peterchappy.lab5.ContentFragmet.onAttach(ContentFragmet.java:83) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:853) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.app.BackStackRecord.run(BackStackRecord.java:833) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452) at android.app.Activity.performStart(Activity.java:5948) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
At the bottom of the auto-generated template there is an inner interface definition called OnFragmentInteractionListener. Your Activityneeds to implement this interface. This is the recommended pattern for your Fragmentto notify your Activityof events so it can then take appropriate action, such as load another Fragment.
In the documentation shows how to implement the listener in the fragment (which is already generated by wizard), but not in the mainactivity which causes the app to crash. – Mario Jul 16 '14 at 11:12 3 Not exactly. The doc (and the generated code) define the interface and check against it when the Activityis attached to the Fragment.
With current version of the SDK in Android Studio, it requiresyou to implement the onFragmentIntereactior(Uri)method, which is not mentioned in any of the other answers. +1 – user3896255 Jun 29 '15 at 18:58 Thanks for mentioning this. Helped me a lot. – hablema Aug 16 '16 at 7:54 Glad that it can help a lot of people.. – Bla... Aug 16 '16 at 7:54 3
Just go to your fragment Activity and remove all method.....instead on on createview method. your fragment has only on method oncreateview that's it. //only this method implement other method delete
The OnFragmentInteractionListener
interfaces in both of your Fragment
s contain a method with the same signature, and MainActivity
is implementing one method to try to cover both interfaces.
Change the signature of one or both of the methods, then implement both methods in MainActivity
. For example:
NavFragment
public interface OnFragmentInteractionListener { public void onNavFragmentInteraction(String string); }
ContentFragment
public interface OnFragmentInteractionListener { public void onContentFragmentInteraction(String string); }
MainActivity
@Override public void onNavFragmentInteraction(String string) { // Do stuff } @Override public void onContentFragmentInteraction(String string) { // Do different stuff }
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