3

I am passing arguments using safeargs. In the reciving fragment I am getting a compile error: 'Required Bundle Found Bundle?'. Cannot see where the error has crept in.

Googled around, checked text and udacity tutorial

Where error appears (at 'arguments')

package com.example.android.naveditoryoutube

import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_two_fragment.*

class FragmentTwo : Fragment() {

    companion object {
        fun newInstance() = FragmentTwo()
    }

    private lateinit var viewModel: FragmentTwoViewModel

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

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProviders.of(this).get(FragmentTwoViewModel::class.java)

    }

    override fun onStart() {
        super.onStart()
        var args = FragmentTwoArgs.fromBundle(arguments)
        argText.text = args.calculated_Number
    }

}

Sending code:

package com.example.android.naveditoryoutube

import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.navigation.Navigation
import kotlinx.android.synthetic.main.fragment_one_fragment.*


class FragmentOne : Fragment() {


    companion object {
        fun newInstance() = FragmentOne()
    }

    private lateinit var viewModel: FragmentOneViewModel


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



        return inflater.inflate(R.layout.fragment_one_fragment, container, false)

    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProviders.of(this).get(FragmentOneViewModel::class.java)

        var calculated_Number : String = viewModel.sendNewNumber().toString()

        button_calculate.setOnClickListener{view: View ->
            if (number_box.text.isNotEmpty()){
                var number_entered: String = number_box.text.toString()
                viewModel.findNewNumber((number_entered))
                calculated_Number = viewModel.sendNewNumber()
               Navigation.findNavController(view).navigate(FragmentOneDirections.actionFragmentOneToFragmentTwo(calculated_Number))
            }


        }
    }

}

Error appears here var args = FragmentTwoArgs.fromBundle(arguments)

arguments is expected to be Bundle but is Bundle?

Kevin Amor
  • 51
  • 1
  • 6

5 Answers5

5

I had to change it to use !!

var args = FragmentTwoArgs.fromBundle(arguments!!)

3

use requireArguments() instead of arguments!!

Kashif Mehmood
  • 323
  • 2
  • 15
  • Could you explain what this does and why it's preferred? – Ellen Spertus Feb 23 '21 at 19:56
  • 1
    https://developer.android.com/reference/androidx/fragment/app/Fragment#requireArguments() check this. However, there is a better option if you are using safe args with nav component just make a property and initialize it like this val args : FragmentArgs by navArgs() – Kashif Mehmood Feb 24 '21 at 06:23
1

try using requireArguments() instead of arguments

var args = FragmentTwoArgs.fromBundle(requireArguments())
sharathbp
  • 11
  • 1
0

You shouldn't use FragmentTwoArgs.fromBundle(arguments) just use:

val args: FragmentTwoArgsby navArgs()

And in your method call:

val amount = args.amount

It's work for me, see official guide: https://developer.android.com/topic/libraries/architecture/navigation/navigation-pass-data

113408
  • 3,364
  • 6
  • 27
  • 54
Dmitry
  • 1
  • 1
  • Hi Dmitry - the compile errors have vanished - Thanks very much – Kevin Amor Feb 07 '19 at 16:45
  • 1
    Using `navArgs()` requires -ktx dependencies, if you aren't using them (e.g. a Java-only project) then the method is `getArguments() `. Also, for `navArgs()` the '`by`' keyword is required... – IainCunningham Mar 03 '19 at 12:17
0

Instead of

var args = FragmentTwoArgs.fromBundle(arguments) argText.text = args.calculated_Number

you can use

arguments?.let {
                  val args = FragmentTwoArgs.fromBundle(it)
                  argText.text = args.calculated_Number
            }
Amir Hossein
  • 45
  • 1
  • 8