涉及循环继承 [英] cyclic inheritance involving

查看:70
本文介绍了涉及循环继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用片段实现导航抽屉.我已经实现了导航抽屉,并且工作正常,但是当我单击片段时,出现错误消息:java.lang.RuntimeException:com.example.saarikakumar.factsappfull.MainActivity@1758ab2必须实现OnFragmentInteractionListener.

I am trying to implement a navigation drawer using fragments. I have the navigation drawer implemented and working well, but when I click on the fragments I get an error saying: java.lang.RuntimeException: com.example.saarikakumar.factsappfull.MainActivity@1758ab2 must implement OnFragmentInteractionListener.

当我尝试在片段中实现此方法和所需的方法时,它告诉我涉及该片段的循环继承.我在下面发布了我的片段代码:

When I try to implement this and the required methods in my fragment, it tells me that there is a cyclic inheritance involving the fragment. I am posting my fragment code below:

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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 FirstFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link FirstFragment#newInstance} factory method to
 * create an instance of this fragment.
 */


public class FirstFragment extends Fragment implements FirstFragment.OnFragmentInteractionListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public FirstFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SecondFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FirstFragment newInstance(String param1, String param2) {
        FirstFragment fragment = new FirstFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @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) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    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;
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }


    /**
     * 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
        void onFragmentInteraction(Uri uri);
    }
}

推荐答案

我知道这很旧,但是,您可以从错误中读取-> java.lang.RuntimeException:com.example.saarikakumar.factsappfull.MainActivity @ 1758ab2必须实现OnFragmentInteractionListener.

I know this is old but, as you can read from the error -> java.lang.RuntimeException: com.example.saarikakumar.factsappfull.MainActivity@1758ab2 must implement OnFragmentInteractionListener.

因此MainActivity是需要实现该接口的接口,因此该片段可以调用 mListener.notifyActivityOfSomething()之类的方法.

So MainActivity is the one that needs to implement the interface, so the fragment can call methods as mListener.notifyActivityOfSomething().

这篇关于涉及循环继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆