Java Android RuntimeException:活动必须实现OnFragmentInteractionListener

Java Android RuntimeException:活动必须实现OnFragmentInteractionListener,java,android,Java,Android,我正在尝试使用片段实现抽屉布局 content\u main.xml(应显示片段的位置): fragment\u main.xml: import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.view.View; import android.support.de

我正在尝试使用
片段实现
抽屉布局

content\u main.xml(应显示片段的位置):

fragment\u main.xml:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.FragmentManager;
import layout.FragmentMain;
...other stuff...
 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        FragmentMain fragment_main = FragmentMain.newInstance("one","two");
        FragmentManager manager= getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.FragmentPlaceLayout,fragment_main).commit();
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.FragmentMain">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

</RelativeLayout>
package layout;

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;

import com.daveslab.wideview.R;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link FragmentMain.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link FragmentMain#newInstance} factory method to
 * create an instance of this fragment.
 */
 public class FragmentMain extends Fragment {
 // 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 FragmentMain() {
    // 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 FragmentMain.
 */
// TODO: Rename and change types and number of parameters
public static FragmentMain newInstance(String param1, String param2) {
    FragmentMain fragment = new FragmentMain();
    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_main, 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;
}

/**
 * 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);
}
}
09/25 11:16:55: Launching app
 ....
Success


$ adb shell am start -n      ....

              [ 09-25 09:16:58.565  2534: 2534 D/         ]
              HostConnection::get() New Host Connection established                0xad13bdf0,    tid 2534
I/OpenGLRenderer: Initialized EGL, version 1.4
W/PathParser: Points are too far apart 4.000000596046461
D/AndroidRuntime: Shutting down VM


              --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.daveslab.wideview, PID: 2534
java.lang.RuntimeException:com.daveslab.wideview.MainActivity@aa561a9 must     implement OnFragmentInteractionListener
                  at layout.FragmentMain.onAttach(FragmentMain.java:84)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1043)
                  at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:838)
                  at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:878)
                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:719)
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 I/Process: Sending signal. PID: 2534 SIG: 9
  Application terminated.

一些想法?

让您的
main活动
实现
OnFragmentInteractionListener
接口。

您的活动没有
OnFragmentInteractionListener
因此,如果找不到它,那么您的代码将在
else
案例中抛出此异常本身

if (context instanceof OnFragmentInteractionListener) {
    mListener = (OnFragmentInteractionListener) context;
} else {
    throw new RuntimeException(context.toString() //  Here is your exception
            + " must implement OnFragmentInteractionListener");
}
main活动中执行此操作

public class MainActivity extends AppCompactActivity implements 
              NavigationView.OnNavigationItemSelectedListener
             ,FragmentMain.OnFragmentInteractionListener{

  @Override 
  public void onFragmentInteraction(Uri uri){

  }
}

@DavidEnable使您的活动实现缺少的接口,即
OnFragmentInteractionListener
在您的情况下,仍然存在一个问题:如何实现
implements FragmentMain.OnFragmentInteractionListener
implements NavigationView.OnNavigationItemSelectedListener
之后
implements NavigationView.OnNavigationItemSelectedListener,FragmentMain.OnFragmentInteractionListener
@DavidEnable studio只需按ALT+Enter并选择选项Implemente…使用savedInstanceState解决。
public class MainActivity extends AppCompactActivity implements 
              NavigationView.OnNavigationItemSelectedListener
             ,FragmentMain.OnFragmentInteractionListener{

  @Override 
  public void onFragmentInteraction(Uri uri){

  }
}