转载请注明出处:http://blog.csdn.net/parallelyk/article/details/51510569

今天写代码的时候发现,用Android Studio创建一个blank Fragment的时候会为我们创建一个Fragment的模板。

public class MainFragment extends Fragment {// TODO: Rename parameter arguments, choose names that match// the fragment initialization parameters, e.g. ARG_ITEM_NUMBERprivate static final String ARG_PARAM1 = "param1";private static final String ARG_PARAM2 = "param2";// TODO: Rename and change types of parametersprivate String mParam1;private String mParam2;private OnFragmentInteractionListener mListener;public MainFragment() {// 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 MainFragment.*/// TODO: Rename and change types and number of parameterspublic static MainFragment newInstance(String param1, String param2) {MainFragment fragment = new MainFragment();Bundle args = new Bundle();args.putString(ARG_PARAM1, param1);args.putString(ARG_PARAM2, param2);fragment.setArguments(args);return fragment;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (getArguments() != null) {mParam1 = getArguments().getString(ARG_PARAM1);mParam2 = getArguments().getString(ARG_PARAM2);}}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_main, container, false);}// TODO: Rename method, update argument and hook method into UI eventpublic void onButtonPressed(Uri uri) {if (mListener != null) {mListener.onFragmentInteraction(uri);}}@Overridepublic void onAttach(Context context) {super.onAttach(context);if (context instanceof OnFragmentInteractionListener) {mListener = (OnFragmentInteractionListener) context;} else {throw new RuntimeException(context.toString()+ " must implement OnFragmentInteractionListener");}}@Overridepublic 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 namevoid onFragmentInteraction(Uri uri);}
}

其中有一段:

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

即我们的context也就是Activity必须实现OnFragmentInteractionListener。

那么OnFragmentInteractionListener是什么东西?

在一个Activity中往往有多个Fragment,他们之间是无法直接通信的,所以Fragment需要通过它们所绑定的Activity作为中介来进行通信。而OnFragmentInteractionListener就是用来实现不同Fragment之间通信的接口。OnFragmentInteractionListener在OnAttach阶段注册并复制给mListener。

使用:在Fragment中直接调用mListener.OnFragmentInteractionListener(uri)

mListener.OnFragmentInteractionListener(uri)

在Activity中通过

    @Overridepublic void onFragmentInteraction(Uri uri) {//做想要做的数据操作和通信,如给其他Fragment发送数据sendMsgTo(otherFragment,uri);}

Exception:must implement OnFragmentInteractionListener相关推荐

  1. must implement OnFragmentInteractionListener/ Fragment与Activity,Fragment与Fragment之间的信息传递

    出现这个问题的原因, 是Fragment关联的Activity没有实现OnFragmentInteractionListener接口. 那为什么要实现这个接口,以及怎样实现这个接口呢?让我们一步一步来 ...

  2. python access violation_python – “WindowsError:exception:access violation …” – ctypes问题...

    以下是驻留在DLL中的C函数的原型: extern "C" void__stdcall__declspec(dllexport) ReturnPulse(double*,doubl ...

  3. Flutter开发:运行flutter upgrade命令报错Exception:Flutter failed to create a directory at…解决方法

    前言 在Flutter开发中,经常会遇到因为更新而引起的报错,而且最近两年Flutter不断完善和更新的速度越来越快,这就需要Flutter相关的开发者时刻保持不断学习的心态,来应对在Flutter开 ...

  4. java fatal exception_如何解决FATAL EXCEPTION:pool-4-thread-1错误?

    我有一个工作得很好的应用程序,但有时候我在程序中添加了一些类和代码,导致我的应用程序崩溃.在我logcat错误是这样的:如何解决FATAL EXCEPTION:pool-4-thread-1错误? F ...

  5. exception:Deleted row information cannot be accessed through the row处理

    如图: exception说的比较清楚了,已经从list里删除的这行就不能再读取了, 所以在读取之前做一个判断去处理就可以了: For Each coderow As CourseDataset.T_ ...

  6. Ruby on Rails Exception:Routing Error

    当开始做Ruby on Rails实际编程时,对于初学者而言总会有些让人感到未知的Exception, 上次以手工方式写了一个简单的Rails Application, 今天下午换用Red Rails ...

  7. net.sf.json.exception:Unquotted String错误,解决办法。

    错误展现: $parameter='{"iq":{"namespace":"FormSendDoRequest","query&q ...

  8. Kafka Exception:Bootstrap broker disconnected Consumer disconnected

    还会导致一些前端服务不可用的现象,都是正常的 解决方案:删除容器重新部署

  9. Exception:java.lang.IllegalArgumentException: invalid comparison

    异常: java.lang.IllegalArgumentException: invalid comparison Error querying database. Cause: java.lang ...

  10. check在java意思吗_谈谈Java:Checked Exception与 unCheckException Runtime Exception 的区别...

    Java里有个很重要的特色是Exception ,也就是说允许程序产生例外状况.而在学Java 的时候,我们也只知道Exception 的写法,却未必真能了解不同种类的Exception 的区别. 首 ...

最新文章

  1. 自然划分的3-4-5规则
  2. NeurIPS | 3D-BoNet:3D 点云实例分割的新框架
  3. Chrome 键盘快捷键(Mac)
  4. 51nod 1004 【快速幂】
  5. linux网络编程之Socket编程
  6. linux中线程的挂起与恢复(进程暂停)
  7. s5pv210——I2C基础详解、I2C时序详解
  8. Kubernetes复杂吗?Cube-新的容器管理服务产品
  9. Ubuntu 19.10 停止支持 32 位的 x86 架构
  10. 使用Maven构建Struts2项目
  11. DataTable增加行
  12. GDAL2.x与1.x的主要变化比较(以C++为例说明)
  13. matlab 稀疏编码,稀疏编码怎样进行图像的特征提取
  14. SIM800C实验记录之通过GPRS上网
  15. 译——基于微服务和AI重新定义软件测试
  16. Exception [WinError 995] 由于线程退出或应用程序请求 的解决办法
  17. scroll-view 下滑列表加载的使用
  18. DAG 图关键路径算法
  19. [Power]Mockito使用和扩展
  20. java 打印表格文件

热门文章

  1. python删除停用词_删除停用词
  2. android 敏感词过滤器,android 过滤敏感词主要类
  3. 调试bootmgrwinload vistawin7 x86x64
  4. 有限域的加减乘除运算
  5. 软件开发中的非功能需求类型
  6. 大年三十整理的asp.net资料!(不得不收藏)
  7. Nginx系列一:Nginx介绍与Nginx工作原理分析
  8. 汪学明导师—商业模式创新与转型专家
  9. 生物流体力学及血流动力学建模仿真技术实战
  10. (Scrapy框架)爬虫2021年CSDN全站综合热榜标题热词 | 爬虫案例