我在一个活动中使用了多个片段,并且在添加另一个片段之后,我添加了第一个,我得到的是自动生成的'必须实现OnFragmentInteractionListener'例外。该应用程序最初没有出现此错误,但在第二次运行后会抛出异常。据我所知,我已正确实施它们。任何帮助将不胜感激。
MainActivity:
package com.example.tom.navigationdrawertest;
import android.app.Activity;
import android.net.Uri;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import com.parse.Parse;
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks, ExploreFragment.OnFragmentInteractionListener, ActivityFragment.OnFragmentInteractionListener {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
private ExploreFragment mExploreFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
@Override
public void onActivityFragmentInteraction(Uri uri){}
@Override
public void onExploreFragmentInteraction(Uri uri){}
// onCreate() etc etc
ActivityFragment:
//rest of the code
@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 onActivityFragmentInteraction(Uri uri);
}
}
ExploreFragment:
//rest of code
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onExploreFragmentInteraction(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 onExploreFragmentInteraction(Uri uri);
}
答案 0 :(得分:0)
您没有添加正确的片段。错误的含义是您的新Activity
必须是OnFragmentInteractionListener
的类型,您添加的第一个是正常的,但第二个不是。
onAttach方法必须返回OnFragmentInteractionListener Activity
,例如MainActivity
。