如何实现OnFragmentInteractionListener


151

我在Android Studio 0.8.2中使用向导生成了带有导航抽屉的应用程序

我创建了一个片段,并将其添加到newInstance()中,并出现以下错误:

com.domain.myapp E / AndroidRuntime:致命例外: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));
    }
}

}

Answers:


120

此处发布的答案无济于事,但以下链接起作用了:

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更新:8/31/2015

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

7
请注意,onAttach(Activity activity);已弃用,并替换为onAttach(Context context)
EpicPandaForce

1
@EpicPandaForce确实您是对的,我更新了帖子以反映这一点
meda 2015年

1
它从onAttach转到onStart有什么原因吗?我是否仍可以仅使用上下文而不是活动将其放在onAttach中?

我认为只使用onAttach(context)就可以了
-EpicPandaForce

212

对于那些在阅读@meda答案后仍然不明白的人,以下是我对此问题的简洁明了的解释:

假设您有2个片段, Fragment_A以及Fragment_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
    }
}

PS:简而言之,此方法可用于片段之间的通信。对于那些想进一步了解此方法的人,请参考此链接


10
在Android Studio中使用最新版本的SDK时,它要求您实现该onFragmentIntereactior(Uri)方法,其他任何答案均未提及。+1

2
非常感谢你!
ofir_aghai

感谢您提及这一点。对我有很大帮助。
hablema '16

2
需要吗?您可以删除与侦听器相关的代码...如果碰巧不需要与其他片段进行交互的片段,则这些侦听器将无用。
追踪

4
比接受的答案容易理解和理解。
jerrythebum

44

查看Fragment由Android Studio 自动生成的内容。创建新的时Fragment,Studio会为您存入一堆代码。在自动生成的模板的底部,有一个内部接口定义,称为OnFragmentInteractionListener。您Activity需要实现此接口。建议您使用此模式Fragment来通知您Activity事件,以便随后采取适当的操作,例如加载another Fragment。请参阅此页面以获取详细信息,寻找“为Activity创建事件回调”部分:http : //developer.android.com/guide/components/fragments.html


在文档中显示了如何在片段(向导已经生成)中实现侦听器,但没有在导致应用崩溃的mainactivity中实现侦听器。
Mario M

3
不完全是。doc(和生成的代码)定义了接口,并在将Activity附加到时对其进行检查Fragment。您看到的崩溃是因为您Activity尚未实现该接口。您需要进入Activity并添加,implements YourFragment.OnFragmentInteractionListener然后添加接口中定义的方法的实现。
拉里·谢弗

好的,但是我不知道如何添加该实现,因为它不在android sdk文档中
Mario M

1
它不是SDK的一部分,而是最佳实践。向导生成的模板代码只是为您奠定基础。该接口onFragmentInteraction(Uri uri)只是一个存根。您可以根据需要以及Activity实现它的需要使此方法成为任何对象。看看有助于。
拉里·谢弗

3
这个提示节​​省了许多时间。创建片段时,请检查“包括片段工厂方法”和“包括接口回调”。而且您不必实现OnFragmentInteractionListener。我正在将Android Studio 1.3.2与Java sdk 8一起使用。Android6.0(API 23)和sdk-platform 23是。谢谢拉里·谢弗(Larry Schiefer)。
学习者2015年

28

对于那些访问此页面并希望进一步澄清该错误的人,在我的情况下,调用该片段的活动在这种情况下需要具有2个实现,例如:

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

9

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

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

界面/监听器是默认创建的,因此您的活动和片段可以更轻松地进行交流


2
这是一个很好的观点,因为大多数初学者应用程序都不需要此侦听器。
法学徒

5

除了@ 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底部的物品


5

与我一起工作删除此代码:

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

4

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

简而言之,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


3

代替Activity使用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");
        }
}

3

只是一个附录:

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

目的是您可以将片段附加到多个活动,并且仍然重复使用相同的通信方法(每个活动可能对每个片段都有其自己的OnFragmentInteractionListener)。

但是,如果我确定我的片段将仅附着于一种类型的活动,并且我想与该活动进行交流?

然后,如果您由于OnverbInteractionListener的冗长性而不想使用它,则可以使用以下方法访问您的活动方法:

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

尽管这在大多数情况下可行,但是如果片段在调用时已与活动分离,则有时getActivity()可以返回null,例如,在asynctask的postExecute方法中,如果调用get活动但已经离开了片段在asyncTask完成之前,您将获得空指针异常。因此,android文档专门说要使用片段交互侦听器接口
MichaelStoddart

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

并确保您的布局是适合您的演示。


谢谢...如果有需要kalpeshnikam1080@gmail.com投递邮件
Kalpesh A. Nikam '18

1

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

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

以及在上下文中使用新的onStart()方法时

@Override
public void onDestroy() {
    super.onDestroy();
    mListener = null;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.