如何实现OnFragmentInteractionListener

时间:2014-07-16 10:05:55

标签: android android-fragments

我在android studio 0.8.2

中有一个带导航抽屉的向导生成的应用程序

我创建了一个片段,并使用newInstance()添加它,我收到此错误:

  

com.domain.myapp E / AndroidRuntime:FATAL EXCEPTION:main       java.lang.ClassCastException:com.domain.myapp.MainActivity@422fb8f0必须实现   OnFragmentInteractionListener

我找不到任何地方如何实现这个OnFragmentInteractionListener? 即使在android sdk文档中也找不到它!

MainActivity.java

import android.app.Activity;

import android.app.ActionBar;
import android.app.Fragment;
import android.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;


public class MainActivity extends Activity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();

    switch (position) {
        case 0: fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit(); break; 
        case 1: fragmentManager.beginTransaction() 
                .replace(R.id.container, AboutFragment.newInstance("test1", "test2"))
                .commit(); break; // this crashes the app
        case 2: fragmentManager.beginTransaction()
                .replace(R.id.container, BrowseQuotesFragment.newInstance("test1", "test2"))
                .commit(); break; // this crashes the app
    }
}


public void onSectionAttached(int number) {
    switch (number) {
        case 1:
            mTitle = getString(R.string.title_section1);
            break;
        case 2:
            mTitle = getString(R.string.title_section2);
            break;
        case 3:
            mTitle = getString(R.string.title_section3);
            break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.main, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

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

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

}

12 个答案:

答案 0 :(得分:193)

对于那些在阅读@meda回答后仍然不了解的人,以下是我对这个问题的简明扼要的解释:

我们假设您有2个片段,Fragment_AFragment_B,这些片段是从应用中自动生成的。在生成的片段的底部,您将找到此代码:

public class Fragment_A extends Fragment {

    //rest of the code is omitted

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }
}

public class Fragment_B extends Fragment {

    //rest of the code is omitted

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }
}

要解决此问题,您必须在活动中添加onFragmentInteraction方法,在我的案例中,该方法名为MainActivity2。之后,您需要implements中的所有片段MainActivity,如下所示:

public class MainActivity2 extends ActionBarActivity
        implements Fragment_A.OnFragmentInteractionListener, 
                   Fragment_B.OnFragmentInteractionListener, 
                   NavigationDrawerFragment.NavigationDrawerCallbacks {
    //rest code is omitted

    @Override
    public void onFragmentInteraction(Uri uri){
        //you can leave it empty
    }
}

P.S。:简而言之,这种方法可用于片段之间的通信。如果您想了解有关此方法的更多信息,请参阅此link

答案 1 :(得分:110)

此处发布的答案没有帮助,但以下链接确实:

<强> http://developer.android.com/training/basics/fragments/communicating.html

定义接口

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    ...
}

例如,当用户单击列表项时,将调用片段中的以下方法。该片段使用回调接口将事件传递给父活动。

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Send the event to the host activity
    mCallback.onArticleSelected(position);
}

实施界面

例如,以下活动实现了上例中的接口。

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article
    }
}

API 23更新:2015年8月31日

onAttach(Activity activity)现已弃用了覆盖方法android.app.Fragment,代码应升级为onAttach(Context context)

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}


@Override
public void onStart() {
    super.onStart();
    try {
        mListener = (OnFragmentInteractionListener) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException(getActivity().toString()
                + " must implement OnFragmentInteractionListener");
    }
}

答案 2 :(得分:41)

查看由Android Studio创建的自动生成的Fragment。当您创建新的Fragment时,Studio会为您创建一堆代码。在自动生成的模板的底部有一个名为OnFragmentInteractionListener的内部接口定义。您的Activity需要实现此界面。这是Fragment通知您Activity事件的推荐模式,以便它可以采取适当的操作,例如加载另一个Fragment。有关详细信息,请参阅此页面,查找&#34;为活动&#34;创建事件回调。部分:http://developer.android.com/guide/components/fragments.html

答案 3 :(得分:26)

对于那些访问此页面寻找有关此错误的进一步说明的人,在我的情况下,调用片段的活动在这种情况下需要有2个实现,如下所示:

public class MyActivity extends Activity implements 
    MyFragment.OnFragmentInteractionListener, 
    NavigationDrawerFragment.NaviationDrawerCallbacks {
    ...// rest of the code
}

答案 4 :(得分:8)

您应尝试从片段中删除以下代码

    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }

接口/侦听器是默认创建的,以便您的活动和片段可以更轻松地进行通信

答案 5 :(得分:4)

除了@ user26409021的回答,如果您添加了ItemFragment,则ItemFragment中的消息为;

Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener} interface.

你应该加入你的活动;

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, ItemFragment.OnListFragmentInteractionListener {

//the code is omitted

 public void onListFragmentInteraction(DummyContent.DummyItem uri){
    //you can leave it empty
}

这里的虚拟物品就是你在ItemFragment底部的物品

答案 6 :(得分:3)

而不是活动使用context.It对我有用。

@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mListener = (OnFragmentInteractionListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
}

答案 7 :(得分:3)

只是一个附录:

OnFragmentInteractionListener使用接口(OnFragmentInteractionListener)处理Activity和Fragment之间的通信,默认情况下由Android Studio创建,但如果您不需要与您的活动进行通信,则可以获得骑它。

目标是您可以将片段附加到多个活动并仍然重复使用相同的通信方法(每个活动都可以为每个片段创建自己的OnFragmentInteractionListener)。

但是,如果我确定我的片段只会附加到一种类型的活动,并且我想与该活动进行通信?

然后,如果您不想使用OnFragmentInteractionListener,因为它的详细程度,您可以使用以下方法访问您的活动方法:

((MyActivityClass) getActivity()).someMethod()

答案 8 :(得分:3)

OnFragmentInteractionListener是处理片段到活动通信的默认实现。这可以根据您的需求实施。假设您的活动中需要一个函数在片段中的特定操作期间执行,您可以使用此回调方法。如果您不需要在托管activityfragment之间进行此互动,则可以删除此实施。

简而言之,如果您需要像这样的片段活动交互,那么您应该implement片段托管活动中的监听器

public class MainActivity extends Activity implements 
YourFragment.OnFragmentInteractionListener {..}

并且您的片段应该像这样定义

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

还提供了活动中void onFragmentInteraction(Uri uri);的定义

或者如果您没有任何片段 - 活动互动,只需从您的片段listener中删除onAttach初始化

答案 9 :(得分:2)

只需转到您的片段Activity并删除所有方法.....而不是createview方法。

您的片段只有oncreateview方法。

///仅此方法实现其他方法的删除

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

并确保您的布局是u的演示。

答案 10 :(得分:1)

当片段与活动分离或被破坏时,我想添加对侦听器的破坏。

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

以及使用带有Context的新onStart()方法

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

答案 11 :(得分:0)

与我一起工作,删除了这段代码:

@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 onAttach(Context context) {
    super.onAttach(context);
}