F
F
foonfyrick2021-04-17 10:24:35
Android
foonfyrick, 2021-04-17 10:24:35

Can't transfer text from one fragment to the textview of another?

Cheto I got confused, this.tv_top must not be null always comes.
It seems that I used to transfer values, but now I can’t remember.
The screen is created and displayed, but in the override fun passData method, if you check, it says that the fragment has not even been added to the screen, and the view does not exist.

class BotFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_bot, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        this.setTargetFragment(TopFragment(),1)
        val tf = targetFragment as ITop

        button_bot.setOnClickListener {
            tf.passData("@@@@@@@@@@")
        }
    }

}


class TopFragment : Fragment(),ITop {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        return inflater.inflate(R.layout.fragment_top, container, false)
    }


    override fun passData(data: String) {
        this.tv_top.text = data
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-04-17
@foonfyrick


this.setTargetFragment(TopFragment(),1)
val tf = targetFragment as ITop

It makes no sense, you just created a fragment and stuffed it as a target without adding a manager to the fragment. Of course, its life cycle has not started, the view has not been created, and these synthetic properties of yours have not been initialized. That's why you catch NPE.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question