我正在尝试将recyclerview项目从活动传递到片段,我发现Bundle对此很有用,但是当我尝试检索数据然后应用崩溃时,在我的片段中,该如何解决?
这是我发送数据的活动文件代码
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);
这是我的片段活动(名为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;
}
}
现在,当我删除用于检索标题,描述和日期的行时,该应用程序可以正常运行,但是在添加它们时,该应用程序崩溃了。
答案 0 :(得分:0)
您可以通过此方法使用
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
}