Android RuntimeException:Activity必须实现OnFragmentInteractionListener

时间:2016-09-25 09:20:22

标签: java android

我正在尝试使用drawer layout实现fragments

content_main.xml (应显示片段):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.daveslab.wideview.MainActivity"
tools:showIn="@layout/app_bar_main"
android:id="@+id/FragmentPlaceLayout">
</RelativeLayout>

MainActivity.java:

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;
}

fragment_main.xml:

<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>

FragmentMain.java:

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.

一些想法?

2 个答案:

答案 0 :(得分:3)

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

if (context instanceof OnFragmentInteractionListener) {
    mListener = (OnFragmentInteractionListener) context;
} else {
    throw new RuntimeException(context.toString() //  Here is your exception
            + " must implement OnFragmentInteractionListener");
}

MainActivity

中执行此操作
public class MainActivity extends AppCompactActivity implements 
              NavigationView.OnNavigationItemSelectedListener
             ,FragmentMain.OnFragmentInteractionListener{

  @Override 
  public void onFragmentInteraction(Uri uri){

  }
}

答案 1 :(得分:0)

OnFragmentInteractionListener实施<div id="cone" class="team"> <div class="default"></div> ... <div class="default"> <img src="img.jpg" width="271" height="271" alt="" border="0"> First Text line<br> Second text line (optional) <img src="img.jpg" width="271" height="271" alt="" border="0"> First Text line<br> Second text line (optional) ... </div> </div>界面。