gpt4 book ai didi

java - 将数据从 ListView 中单击的项目传递到另一个 Activity 时出错

转载 作者:行者123 更新时间:2023-12-02 03:59:30 25 4
gpt4 key购买 nike

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

为此,我在 Intent 中传递了单击项目的位置。但是,应用程序在执行时崩溃

这是历史 fragment 代码

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)”

最佳答案

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

关于java - 将数据从 ListView 中单击的项目传递到另一个 Activity 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56740855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com