gpt4 book ai didi

java - Android:总是在 Intent 中传递的额外内容中接收空对象

转载 作者:行者123 更新时间:2023-12-01 19:30:05 26 4
gpt4 key购买 nike

我正在我的应用程序中创建一个回收器 View ,并从 firestore 中获取相同的数据。在此过程中,我将一些参数传递给单击回收器 View 中的项目时调用的 Intent 。

RecyclerView recyclerView = findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

adapter.setOnItemClickListener(new LearningModuleAdapter.OnItemClickListener() {
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
LearningModule learningModule = documentSnapshot.toObject(LearningModule.class);
String id=documentSnapshot.getId();
String path = documentSnapshot.getReference().getPath();
String title = (String) documentSnapshot.get("title");
System.out.println("what did i get from document snapshot? id: "+id+" path:"+path+" title:"+title);
Intent intent = new Intent(learn.this,learn_content.class);
intent.putExtra("title",title);
startActivity(intent);
}
});

上述代码中 print 语句的输出如下:

what did i get from document snapshot? id: EsHW9FGjVe8A3pG4 path:LearningModules/EsHW9FGjVe8A3pG4 title:Front Load vs Top Load

被调用 Activity 的 onClick 方法如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_learn_view_pager);
viewPager = findViewById(R.id.pager);
progressBar = findViewById(R.id.progressBarViewPager);
Bundle extras;
extras = getIntent().getExtras();
System.out.println("captured form the first " + extras.getString("title"));

但是,这段代码会为 print 语句抛出错误 NullPointerException

我哪里出错了?

附加代码:

  • 适配器
public class LearningModuleAdapter extends FirestoreRecyclerAdapter<LearningModule, LearningModuleAdapter.LearningModuleViewHolder> {
private OnItemClickListener listener;


public LearningModuleAdapter(@NonNull FirestoreRecyclerOptions<LearningModule> options) {
super(options);
}


@Override
protected void onBindViewHolder(@NonNull final LearningModuleViewHolder holder, final int position, @NonNull LearningModule model) {
holder.title.setText(model.getTitle());
// holder.img.setImageResource(model.getImg());

}

@NonNull
@Override
public LearningModuleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new LearningModuleViewHolder(v);
}


public class LearningModuleViewHolder extends RecyclerView.ViewHolder {
public View view;
public TextView title;
public ImageView img;

public LearningModuleViewHolder(View v) {
super(v);
view = v;
img = itemView.findViewById(R.id.list_image);
title = itemView.findViewById(R.id.list_text_title);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("The click was captured");
int position = getAdapterPosition();
if(position!=RecyclerView.NO_POSITION && null!=listener){
listener.onItemClick(getSnapshots().getSnapshot(position),position);
}
Intent intent = new Intent(view.getContext(), learn_content.class);
view.getContext().startActivity(intent);
}
});
}

}

public interface OnItemClickListener{
void onItemClick(DocumentSnapshot documentSnapshot,int position);
}

public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}


}
  • 错误堆栈跟踪
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gigforce.app/com.gigforce.app.learn_content}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.gigforce.app.learn_content.onCreate(learn_content.java:45)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356)

最佳答案

您调用 startActivity 两次导致了问题。

选项 - 1:尝试删除 itemView.setOnClickListener 内部,如下所示:

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("The click was captured");
int position = getAdapterPosition();
if(position!=RecyclerView.NO_POSITION && null!=listener){
listener.onItemClick(getSnapshots().getSnapshot(position),position);
}

//Intent intent = new Intent(view.getContext(), learn_content.class);
//view.getContext().startActivity(intent);
}
});

选项 - 2:尝试在 itemView.setOnClickListener 内启动 Activity 并删除 OnItemClickListener 相关代码,如下所示:

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("The click was captured");
int position = getAdapterPosition();

/*if(position!=RecyclerView.NO_POSITION && null!=listener){
listener.onItemClick(getSnapshots().getSnapshot(position),position);
}*/

Intent intent = new Intent(view.getContext(), learn_content.class);

DocumentSnapshot documentSnapshot = getSnapshots().getSnapshot(position);
String title = (String) documentSnapshot.get("title");

intent.putExtra("title",title);
view.getContext().startActivity(intent);
}
});

关于java - Android:总是在 Intent 中传递的额外内容中接收空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60023975/

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