[英]App crashes on passing data from activity to fragment
I was working on passing the recyclerview item from activity to fragment and I found Bundle useful for that but in my fragment when I try to retrieve the data then the app crashes, so how do I fix it? 我正在尝试将recyclerview项目从活动传递到片段,我发现Bundle对此有用,但是当我尝试检索数据时,在我的片段中应用程序崩溃了,那么如何解决呢?
This is my activity file code for sending the data 这是我发送数据的活动文件代码
Bundle bundle = new Bundle();
bundle.putString("title", title.getText().toString());
bundle.putString("description",
description.getText().toString());
bundle.putString("date", date.getText().toString());
UpdatesActivity updatesActivity = new UpdatesActivity();
updatesActivity.setArguments(bundle);
This is my fragment activity(named UpdatesActivity) 这是我的片段活动(名为UpdatesActivity)
private List<Info> infoList = new ArrayList<>();
private InfoAdapter mAdapter;
private Button btnLogOut;
private FirebaseAuth firebaseAuth;
private String title, description, date;
public UpdatesActivity() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_updates, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
title = getArguments().getString("title");
description = getArguments().getString("description");
date = getArguments().getString("date");
mAdapter = new InfoAdapter(infoList);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(mAdapter);
return view;
}
}
Now when I remove the lines for retrieving the title, description and date then the app works fine but on adding them the app crashes. 现在,当我删除用于检索标题,描述和日期的行时,该应用程序可以正常工作,但是在添加它们时,该应用程序崩溃了。
you can use from this method 你可以用这种方法
private void loadFragment(Fragment fragment) {
Bundle bundle = new Bundle();
bundle.putString("title", title.getText().toString());
bundle.putString("description",
description.getText().toString());
bundle.putString("date", date.getText().toString());
fragment.setArguments(bundle);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, fragment);
fragmentTransaction.commit(); // save the changes
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.