简体   繁体   中英

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

Having issues working with a few fragments.

Writing all this kotlin.

I have a MainActivity (Single Activity App). 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.

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. Within those fragments I am trying to add data to a list using a fab button and an alert dialog. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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