3

我正在使用 safeargs 传递参数。在接收片段中,我收到编译错误:'Required Bundle Found Bundle?'。看不到错误在哪里蔓延。

谷歌搜索,检查文本和 udacity 教程

出现错误的地方(在“参数”处)

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
    }

}

发送代码:

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))
            }


        }
    }

}

此处出现错误 var args = FragmentTwoArgs.fromBundle(arguments)

参数应该是 Bundle 但是 Bundle?

4

5 回答 5

5

我不得不改变它来使用!

var args = FragmentTwoArgs.fromBundle(参数!!)

于 2019-02-02T21:03:54.510 回答
2

使用requireArguments()而不是arguments!!

于 2020-05-27T05:29:34.247 回答
1

尝试使用 requireArguments() 而不是参数

var args = FragmentTwoArgs.fromBundle(requireArguments())
于 2020-08-10T07:14:07.317 回答
0

你不应该FragmentTwoArgs.fromBundle(arguments) 只使用:

val args: FragmentTwoArgsby navArgs()

在您的方法调用中:

val amount = args.amount

这对我有用,请参阅官方指南: https ://developer.android.com/topic/libraries/architecture/navigation/navigation-pass-data

于 2019-02-03T17:54:06.540 回答
0

代替

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

您可以使用

arguments?.let {
                  val args = FragmentTwoArgs.fromBundle(it)
                  argText.text = args.calculated_Number
            }
于 2020-05-07T15:02:15.453 回答