简体   繁体   English

带有父子片段的 Kotlin 中的片段接口空指针异常

[英]Fragment Interface Null Pointer Exception in Kotlin with Parent and Child Fragments

Having issues working with a few fragments.处理一些片段时遇到问题。

Writing all this kotlin.编写所有这些 kotlin。

I have a MainActivity (Single Activity App).我有一个 MainActivity(单一活动应用程序)。 I'm trying to get the parent fragment to communicate a string to the child fragments.我试图让父片段与子片段通信一个字符串。 When following this tutorial it just loops continuously.遵循本教程时,它只是不断循环。 Any other solution I've come across just results in a null pointer exception.我遇到的任何其他解决方案只会导致空指针异常。

Basic communication between two fragments 两个片段之间的基本通信

I'm using android components navigation also, btw.我也在使用 android 组件导航,顺便说一句。

In addition I have to then while using that string send list of objects back to the parent fragment from the child fragments.此外,我必须在使用该字符串时将对象列表从子片段发送回父片段。

Any suggestions?有什么建议?

EDIT:编辑:

clarification, I am running a fragment in bottom nav enviornment that is the parent frag.澄清一下,我正在底部导航环境中运行一个片段,即父片段。 Then, inside that parent frag is a view pager with a tablayout to switch between fragments.然后,在该父片段中是一个带有 tablayout 的视图寻呼机,用于在片段之间切换。 Within those fragments I am trying to add data to a list using a fab button and an alert dialog.在这些片段中,我尝试使用 fab 按钮和警报对话框将数据添加到列表中。 Everything works in the individual fragments it's the data sharing is what I'm struggling with.一切都在单个片段中工作,数据共享是我正在努力解决的问题。 I have not tried the bundle yet as I wasn't sure it would work.我还没有尝试过这个包,因为我不确定它会起作用。 I'll give it a try though.不过我会试试的。

Have you tried using bundle?你试过使用捆绑吗?

var bundle = bundleOf("amount" to amount)
view.findNavController().navigate(R.id.confirmationAction, bundle)

you can get the data in the destination fragment like so您可以像这样获取目标片段中的数据

arguments?.getString("amount")

this is for sending data from parent to child, as for sending it backward I'm not sure if you can actually do that, as far as I know when you press back you just destroy the current fragment and bring up the previous fragment to screen, you aren't creating or calling it again so I don't think you can send any data without actually recreating the parent fragment.这是用于将数据从父级发送到子级,至于向后发送我不确定您是否真的可以这样做,据我所知,当您按回时,您只需销毁当前片段并将前一个片段调出屏幕,您不会再次创建或调用它,因此我认为您无法在不实际重新创建父片段的情况下发送任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM