Android: error interacting with fragment

I looked at stackoverflow to solve my problem and found something very good, but it doesn’t work for me, or I'm just dumb to see it ( How to implement OnFragmentInteractionListener )

I want to place a fragment inside a frame, so I created it, etc. I have to implicate the interface and its method. I (think) I did it, but my application crashes every time ...

* Edit: My application crashes when I call openHome or openRecommended, the onCreate method works, I get these errors, but everything displays correctly.

This is my code:

MainActivity.java

import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity implements HomeFragment.OnFragmentInteractionListener, RecommendedFragment.OnFragmentInteractionListener { FragmentTransaction fragmentTransaction; HomeFragment homeFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); homeFragment = new HomeFragment(); getFragmentManager().beginTransaction().add(R.id.mainFrame, homeFragment).commit(); } protected void openHome(View view) { /*Intent homeIntent = new Intent(this, HomeFragment.class); startActivity(homeIntent); homeFragment = new HomeFragment(); getFragmentManager().beginTransaction().replace(R.id.mainFrame, homeFragment).commit();*/ System.out.println("Success"); } public void openRecommended(View view) { Intent recommendedIntent = new Intent(this, RecommendedFragment.class); startActivity(recommendedIntent); RecommendedFragment recommendedFragment = new RecommendedFragment(); getFragmentManager().beginTransaction().replace(R.id.mainFrame, recommendedFragment).commit(); } @Override public void onFragmentInteractionHome(Uri uri) { } @Override public void onFragmentInteractionRecommended(Uri uri) { } } 

HomeFragment.java

 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; /** * A simple {@link Fragment} subclass. * Activities that contain this fragment must implement the * {@link HomeFragment.OnFragmentInteractionListener} interface * to handle interaction events. * Use the {@link HomeFragment#newInstance} factory method to * create an instance of this fragment. */ public class HomeFragment extends Fragment { private OnFragmentInteractionListener mListener; public static HomeFragment newInstance() { HomeFragment fragment = new HomeFragment(); return fragment; } public HomeFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_home, container, false); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteractionHome(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; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p/> * See the Android Training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments</a> for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name public void onFragmentInteractionHome(Uri uri); } } 

The recommended snippet looks the same, except that some methods have a different name.

I always get this error:

java.lang.RuntimeException: unable to start ComponentInfo activity {com.domain.app/com.domain.app.MainActivity}: java.lang.ClassCastException: com.domain.app.MainActivity@b1d296b0 must implement OnFragmentInteractionListener at android.app.ActivityThread.performLaunchActivity (ActivityThread. java: 2195) at android.app.ActivityThread.access $ 800 (ActivityThread.java:135) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java: 1196) on android.os.Handler.dispatchMessage (Handler.java:102) on android.os.Looper.loop (Looper.java:136) at android.app.ActivityThread.main (ActivityThread.java data 017) in java. lang.reflect.Method.invokeNative (native method) in java.lang.reflect.Method.invoke (Method.java∗15) at com.android.internal.os.Zy goteInit $ MethodAndArgsCaller.run (ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java=95) in dalvik.system.NativeStart.main (native method) Called: java.lang.ClassCastException : com.domain.app.MainActivity@b1d296b0 should implement OnFragmentInteractionListener in com.domain.app.HomeFragment.onAttach (HomeFragment.java:74) in android.app.FragmentManagerImpl.moveToState (FragmentManager.java:849) in android.app.FragmentManagerImpl.moveTomento .java: 1062) on android.app.BackStackRecord.run (BackStackRecord.java:684) in android.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:1447) at android.app.Activity.performStart (Activity.java:5240) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2168) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2245) at android.app.ActivityThread.access $ 800 (ActivityThread.java:135) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1196) on android.os.Handler.dispatchMessage (Handler.java:102) on android.os.Looper.loop (Looper.java:136) at android.app.ActivityThread.main (ActivityThread.java=017) in java.lang.reflect.Method.invokeNative (native method) in java.lang.reflect .Method.invoke (Method.java∗15) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main (ZygoteInit.javahaps95 ) in dalvik.system.NativeStart.main (native method)

I really need help here, I'm stuck for hours ...

John

+6
java android interface android-fragments fragment
source share
2 answers

Ive removed unwanted links and everything related to the RecommendedFragment class. If you publish it, I will update my answer.

Ive checked it and it works. But try to explain what the openHome () method means because it is never used based on your code, and yet you say that the application crashes when you call it.

I added this method to the interface, because it seems to me the most logical.

// MainActivity

 public class MainActivity extends Activity implements HomeFragment.OnFragmentInteractionListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getFragmentManager().beginTransaction() .add(R.id.mainFrame, new HomeFragment()) .commit(); } @Override public void openHome(View view) { System.out.println("Success"); } @Override public void onFragmentInteractionHome(Uri uri) { Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show(); } } 

// HomeFragment

 public class HomeFragment extends Fragment { private OnFragmentInteractionListener mListener; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_home, container, false); } public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteractionHome(uri); } } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnFragmentInteractionListener) activity; mListener.onFragmentInteractionHome(Uri.parse("doWhatYouWant")); } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } public interface OnFragmentInteractionListener { public void onFragmentInteractionHome(Uri uri); public void openHome(View view); } } 
+7
source share

Just remove the OnAttach () method from the snippet

0
source share

All Articles