将数据从列表视图中的单击项目传递到另一个活动时出错

时间:2019-06-24 16:46:34

标签: java android firebase-realtime-database

用日记列表填充Listview之后,单击特定日记后,必须在另一个活动中显示相应的图片。

为此,我在意图中传递了被单击项的位置。但是,该应用在执行时崩溃了

这是历史记录片段代码

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();
}
}

发生以下错误- 尝试在空对象引用上调用虚拟方法'void android.widget.listview.setonitemclicklistener(android.widget.adapterview $ onitemclicklistener)'

1 个答案:

答案 0 :(得分:0)

在调用lv#setOnItemClickListener时,尚未实例化ListView(lv),直到对ValueEventListener#onDataChange的回调发生之前,该列表视图(lv)仍不会被实例化。