使用导航组件传递数据 - 为什么指南不需要时我需要构造函数的值?

Passing data using navigation component - How come I need a values for the constructor when the guide doesn't?

我正在使用 this android guide 在导航目的地之间传递数据。

您可以在下面的代码片段中看到指南不需要任何值传递给构造函数,它设置值而不是传递它们:

@Override
public void onClick(View view) {
   EditText amountTv = (EditText) getView().findViewById(R.id.editTextAmount);
   int amount = Integer.parseInt(amountTv.getText().toString());
   ConfirmationAction action =
           SpecifyAmountFragmentDirections.confirmationAction()
   action.setAmount(amount)
   Navigation.findNavController(view).navigate(action);
}

但是,当我尝试按照此操作时,就像指南所指示的那样,它坚持要我放置我只想设置的传递元素:

这是nav_graph.xml中的相关部分:

目的地:

操作:

我问,因为即使只将它们作为参数传递或将它们作为参数传递并设置它们,程序也声称它没有收到其中一个元素。

您可以使用它来使用 Navigation Architecture Component

传递数据(即 Bundle
EditText amountTv = (EditText) getView().findViewById(R.id.editTextAmount);
int amount = Integer.parseInt(amountTv.getText().toString());
Bundle bundle = new Bundle();
bundle.putInt("AMOUNT",amount);
findNavController().navigate(R.id.IdOfTheFragemntYouAreTryingToNavigate, bundle, null, null)