导航抽屉活动:在片段之间传递值

huangapple go评论108阅读模式
英文:

Navigation Drawer Activity: passing values between fragment

问题

以下是翻译好的内容:

我有一个问题,我无法将一个String值从一个Fragment传递到另一个Fragment

这是我代码的一部分:

FragmentA

  1. public class HomeFragment extends Fragment {
  2. private HomeViewModel homeViewModel;
  3. @Nullable
  4. @Override
  5. public View onCreateView(@NonNull LayoutInflater inflater,
  6. ViewGroup container, Bundle savedInstanceState) {
  7. homeViewModel =
  8. ViewModelProviders.of(this).get(HomeViewModel.class);
  9. View root = inflater.inflate(R.layout.fragment_home, container, false);
  10. Button btn = (Button) root.findViewById(R.id.myButton);
  11. btn.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View v) {
  14. GalleryFragment galleryFragment = new GalleryFragment();
  15. Bundle bundle = new Bundle();
  16. String s = "ciao";
  17. bundle.putString("Data", s);
  18. galleryFragment.setArguments(bundle);
  19. Toast.makeText(getActivity(),s,Toast.LENGTH_LONG).show(); //通过这个我知道函数是正常的
  20. }
  21. });
  22. return root;
  23. }
  24. }

FragmentB

  1. public class GalleryFragment extends Fragment {
  2. private GalleryViewModel galleryViewModel;
  3. public View onCreateView(@NonNull LayoutInflater inflater,
  4. ViewGroup container, Bundle savedInstanceState) {
  5. galleryViewModel =
  6. ViewModelProviders.of(this).get(GalleryViewModel.class);
  7. View root = inflater.inflate(R.layout.fragment_gallery, container, false);
  8. Bundle bundle = getActivity().getIntent().getExtras();
  9. TextView textView = (TextView) root.findViewById(R.id.myTitleText);
  10. if(bundle!=null){
  11. String name = bundle.getString("Data");
  12. Toast.makeText(getActivity(),name,Toast.LENGTH_LONG).show();
  13. textView.setText(name);
  14. }
  15. return root;
  16. }
  17. }

如果我删除if(bundle!=null)部分,调试器会报错:

java.lang.NullPointerException: 尝试调用空对象引用上的 'java.lang.String android.os.Bundle.getString(java.lang.String)' 方法

总结:

我在fragmentA --> 我点击按钮 --> 我移动到fragmentB来查看TextView是否已更改值 --> 什么也没发生。

有人能解释一下错误是什么吗?

谢谢你的时间。

英文:

I have a problem, I can't pass a String value from a Fragment to another Fragment.

This is a part of my code:

The FragmentA:

  1. public class HomeFragment extends Fragment {
  2. private HomeViewModel homeViewModel;
  3. @Nullable
  4. @Override
  5. public View onCreateView(@NonNull LayoutInflater inflater,
  6. ViewGroup container, Bundle savedInstanceState) {
  7. homeViewModel =
  8. ViewModelProviders.of(this).get(HomeViewModel.class);
  9. View root = inflater.inflate(R.layout.fragment_home, container, false);
  10. Button btn = (Button) root.findViewById(R.id.myButton);
  11. btn.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View v) {
  14. GalleryFragment galleryFragment = new GalleryFragment();
  15. Bundle bundle = new Bundle();
  16. String s = "ciao";
  17. bundle.putString("Data", s);
  18. galleryFragment.setArguments(bundle);
  19. Toast.makeText(getActivity(),s,Toast.LENGTH_LONG).show(); //with this i know the function is ok
  20. }
  21. });
  22. return root;
  23. }
  24. }

The FragmentB:

  1. public class GalleryFragment extends Fragment {
  2. private GalleryViewModel galleryViewModel;
  3. public View onCreateView(@NonNull LayoutInflater inflater,
  4. ViewGroup container, Bundle savedInstanceState) {
  5. galleryViewModel =
  6. ViewModelProviders.of(this).get(GalleryViewModel.class);
  7. View root = inflater.inflate(R.layout.fragment_gallery, container, false);
  8. Bundle bundle = getActivity().getIntent().getExtras();
  9. TextView textView = (TextView) root.findViewById(R.id.myTitleText);
  10. if(bundle!=null){
  11. String name = bundle.getString("Data");
  12. Toast.makeText(getActivity(),name,Toast.LENGTH_LONG).show();
  13. textView.setText(name);
  14. }
  15. return root;
  16. }
  17. }

If I delete the if(bundle!=null) part, the debugger give me this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

SUMMARY:

I am on fragmentA --> I click the button --> I move to fragmentB to see if the TextView has changed value --> nothing happened.

Can someone explain me what is the error?

Thank you for your time.

答案1

得分: 0

你应该了解一下 ViewModel:ViewModel 概述

你应该在父活动中初始化一个 ViewModel,然后在每个片段被创建时,提供对该共用 ViewModel 的访问。

这样,这三个组件将共同访问一个共享的生命周期感知对象,你可以用它来进行轻松的互相通信。

英文:

You should look into ViewModel: ViewModel Overview.

You should initialize a ViewModel in your parent activity and then when each Fragment is created, provide access to that common ViewModel.

All 3 components will then have shared access to a common lifecycle-conscious object that you can use for easy intercommunication.

答案2

得分: 0

这是解决方案 -_-,我不知道为什么之前不起作用。

FragmentA 内的 onClick 方法中,在末尾粘贴以下内容:

  1. FragmentTransaction transaction = getFragmentManager().beginTransaction();
  2. transaction.replace(R.id.nav_host_fragment, galleryFragment);
  3. transaction.addToBackStack(null);
  4. transaction.commit();

我发布这个供需要的人使用。

英文:

this is the solution -_-, I dont know why before didnt work.

Inside of FragmentA in the onClick method, paste this at the end:

  1. FragmentTransaction transaction = getFragmentManager().beginTransaction();
  2. transaction.replace(R.id.nav_host_fragment, galleryFragment);
  3. transaction.addToBackStack(null);
  4. transaction.commit();

I post this for who need it

huangapple
  • 本文由 发表于 2020年5月3日 22:52:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/61576502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定