如何在片段之间传递arraylist <string>

时间:2018-07-10 15:38:37

标签: android android-fragments arraylist fragment

我想传递ArrayList,它是Firebase数据库中的数组键。该数组位于OneFragment内部,我想将密钥发送到TwoFragment。 但这给了我这样的错误:

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

这是我的OneFragment代码:

userRef.child(users.getUid()).child("courses").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(final DataSnapshot dataSnapshot) {
                listMatkul.clear();
                keys.clear();
                mProgressBar.setVisibility(View.GONE);
                if (dataSnapshot.exists()){
                    for(DataSnapshot dataHasil : dataSnapshot.getChildren()){
                        final String idCourses = dataHasil.getKey();
                        keys.add(idCourses);

                        Log.e("Yhz",idCourses);
                        mataKuliahRef.child(idCourses).addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot2) {
                                MataKuliah matkul = dataSnapshot2.getValue(MataKuliah.class);
                                listMatkul.add(matkul);
                                mAdapter.notifyDataSetChanged();
                            }



                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });

                    }
                    Bundle bundle=new Bundle();
                    bundle.putStringArrayList("keys", keys);
                    //set Fragmentclass Arguments
                    Fragment fragobj=new TwoFragment();
                    fragobj.setArguments(bundle);

                    FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.asd, fragobj).commit();

                }else{
                }
            }

我像这样在TwoFragment中检索数据:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ArrayList<String> obj2 =getArguments().getStringArrayList("keys");

        Log.e("nba",obj2.toString());
        return inflater.inflate(R.layout.fragment_two, container, false);
    }

0 个答案:

没有答案