必须实现OnFragmentInteractionListener()

时间:2018-05-03 14:15:14

标签: android android-fragments

我知道我不是第一个在这里提出这个问题的人,但不幸的是,正如我在标题中提到的,我在运行应用程序时遇到了错误。但是我已经在我的片段中导入了import android.support.v4.app.Fragment;。 / p>

从父片段我点击它时有一个cardview我需要调用另一个片段来显示与点击的cardview相关的视图。

我已经尝试了很多针对stackoverflow中发布的相同问题的解决方案。但是对我来说没有任何工作。

Reminders.java

public class Reminders extends Fragment {

    private OnFragmentInteractionListener mListener;
    List<reminder> lstreminder;

    public Reminders() {

    }

    public static Reminders newInstance(String param1, String param2) {
        Reminders fragment = new Reminders();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

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

        lstreminder = new ArrayList<>();
        lstreminder.add(new reminder("Alarm",R.drawable.alarms));
        lstreminder.add(new reminder("Birthday",R.drawable.birthdays));
        lstreminder.add(new reminder("Anniversary",R.drawable.anniversaries));
        lstreminder.add(new reminder("Buy Gifts",R.drawable.buygift));
        lstreminder.add(new reminder("Bill Payments",R.drawable.billpayments));
        lstreminder.add(new reminder("Call / SMS",R.drawable.call));
        lstreminder.add(new reminder("Recharges",R.drawable.recharges));
        lstreminder.add(new reminder("Meetings",R.drawable.meetings));
        lstreminder.add(new reminder("Insurance",R.drawable.insurances));
        lstreminder.add(new reminder("Exams",R.drawable.exams));
        lstreminder.add(new reminder("Holidays",R.drawable.holidays));
        lstreminder.add(new reminder("Medical",R.drawable.medical));
        lstreminder.add(new reminder("Purchase",R.drawable.purchase));
        lstreminder.add(new reminder("Travel",R.drawable.travel));
        lstreminder.add(new reminder("Shows",R.drawable.shows));

        RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.mRecyclerView_id);
        final RecyclerviewAdapter recyclerviewAdapter = new RecyclerviewAdapter(getActivity(),lstreminder);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
        recyclerView.setAdapter(recyclerviewAdapter);
        recyclerviewAdapter.setOnItemClickListner(new RecyclerviewAdapter.onItemClickListner() {
            @Override
            public void onClick(reminder str) {
                if(str.getmTitle().equals("Alarm")){
                    Fragment fragment = new Alarm();
                    getFragmentManager().beginTransaction()
                            .replace(R.id.fragmrnt_alarm, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
                }
                recyclerviewAdapter.notifyDataSetChanged();

            }
        });
        return rootView;
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }

}

Alarm.java

public class Alarm extends Fragment {

    private OnFragmentInteractionListener mListener;

    public Alarm() {

    }


    public static Alarm newInstance(String param1, String param2) {
        Alarm fragment = new Alarm();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_alarm, container, false);
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }
}

1 个答案:

答案 0 :(得分:2)

您需要使父活动实现OnFragmentInteractionListener并覆盖该接口提供的方法。

class MyActivity implements OnFragmentInteractionListener

这是你的代码崩溃的地方,你的活动可能没有实现所提到的接口,因此当连接片段时发生了崩溃:

@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }