Kevin Amor
Kevin Amor

Reputation: 51

Using safeargs I get "required Bundle found Bundle?"

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?

Upvotes: 3

Views: 2705

Answers (5)

sharathbp
sharathbp

Reputation: 11

try using requireArguments() instead of arguments

var args = FragmentTwoArgs.fromBundle(requireArguments())

Upvotes: 1

Kashif Mehmood
Kashif Mehmood

Reputation: 303

use requireArguments() instead of arguments!!

Upvotes: 3

Amir Hossein
Amir Hossein

Reputation: 45

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
            }

Upvotes: 0

Dmitry
Dmitry

Reputation: 1

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

Upvotes: 0

jenndotcodes
jenndotcodes

Reputation: 81

I had to change it to use !!

var args = FragmentTwoArgs.fromBundle(arguments!!)

Upvotes: 5

Related Questions