简体   繁体   中英

Error while passing data from clicked item in listview to another activity

After populating the Listview with the list of diaries, upon clicking a particular diary, the corresponding pictures have to be displayed in another activity.

For this, I'm passing the position of clicked item in the intent. But, the app crashes upon execution

This is the history fragment code

public class HistoryFragment extends Fragment
{
ListView lv;
Context context;
public HistoryFragment()
{
    //default constructor
}

//The following method will get the context from the activity to which the fragment is attached
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.context=context;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
    final View view = inflater.inflate(R.layout.scrolllist, container, false);
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference uidRef = rootRef.child("image").child(uid);
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //List<String> cities = new ArrayList<>();
            //cities = new ArrayList<>();
            final ArrayList<Word> cities = new ArrayList<>();
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String cityName = ds.getKey();
                cities.add(new Word(cityName));
            }
            lv = (ListView)view.findViewById(R.id.list_of_ds);
            ContentAdapter arrayAdapter = new ContentAdapter(context,cities);
            lv.setAdapter(arrayAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError)
        {
            //Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
        }
    };
    uidRef.addListenerForSingleValueEvent(valueEventListener);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override

        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            //String diary_name=title.get(position).toString();
            Intent myIntent = new Intent(view.getContext(), DiaryViewActivity.class);
            myIntent.putExtra("diaryname", lv.getItemAtPosition(position).toString());
            startActivity(myIntent);

        }
    });
    return view;
}
}




This is the activity to which intent is passed
//DiaryViewActivity.java


public class DiaryViewActivity extends AppCompatActivity implements       IFirebaseLoadDone
{

ViewPager viewPager;
MAdapter adapter;
DatabaseReference diaries;
IFirebaseLoadDone iFirebaseLoadDone;
FirebaseAuth mAuth=FirebaseAuth.getInstance();
//final ArrayList<Word> word = new ArrayList<Word>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_diary_view);
    Bundle bd = getIntent().getExtras();
    String myVal = bd.getString("diaryname");
    String currentUser;
    currentUser = mAuth.getCurrentUser().getUid();
    diaries = FirebaseDatabase.getInstance().getReference().child("image").child(currentUser)
            .child(myVal);

    iFirebaseLoadDone = this;

    loadDairy();

    viewPager = (ViewPager)findViewById(R.id.view_pager);
    viewPager.setPageTransformer(true,new DepthPageTransformer());

}

private void loadDairy() {
    diaries.addListenerForSingleValueEvent(new ValueEventListener() {
        List<Display> diaryList = new ArrayList<>();
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot data:dataSnapshot.getChildren()) {
                Display d = data.getValue(Display.class);
                //word.add(new Word(d.dname));
                diaryList.add(d);
            }
            iFirebaseLoadDone.onFirebaseLoadSuccess(diaryList);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            iFirebaseLoadDone.onFirebaseLoadFailed(databaseError.getMessage());
        }
    });
}

@Override
public void onFirebaseLoadSuccess(List<Display> diaryList) {

    adapter =  new MAdapter(this,diaryList);
    viewPager.setAdapter(adapter);
}

@Override
public void onFirebaseLoadFailed(String message) {
    Toast.makeText(this,"error! "+message,Toast.LENGTH_SHORT).show();
}
}

The following error occurs - Attempt to invoke virtual method 'void android.widget.listview.setonitemclicklistener(android.widget.adapterview$onitemclicklistener)' on a null object reference

在您调用lv#setOnItemClickListener ,您的ListView(lv)尚未实例化,直到发生对ValueEventListener#onDataChange的回调。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM