gpt4 book ai didi

java - 单击recyclerView新 Activity 尝试调用虚拟方法时出错

转载 作者:行者123 更新时间:2023-12-01 17:07:51 26 4
gpt4 key购买 nike

我需要帮助,对于 onclick recyclerview 并打开新 Activity ,我在项目列表中单击关闭应用程序,logcat 错误是:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String android.content.Context.getPackageName()”

我的代码:

 @Override
public void onBindViewHolder(final estadoView estadoView, final int i) {
final Estado estado = estadoList.get(i);
estadoView.txtnombreMostrar.setText(estado.getStateName());



estadoView.txtnombreMostrar.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(context, Invernadero.class);
context.startActivity(intent);
} });


}

我的公开课是:

public class estadoView extends RecyclerView.ViewHolder {
private TextView txtnombreMostrar;

RelativeLayout parentLayout;


public estadoView(@NonNull View itemView) {
super(itemView);
txtnombreMostrar = itemView.findViewById(R.id.txtNombreMostrar);

}

}

有什么问题吗?我在网络、论坛中搜索,但没有解决我的问题

Code enter image description here

LogCat enter image description here

context is null, how to solved? enter image description here

最佳答案

您似乎遇到了上下文问题,但由于您没有提供如何获取上下文,因此现在将其在 View 监听器中替换为 v.getContext() ,如下所示:

 @Override
public void onBindViewHolder(final estadoView estadoView, final int i) {
final Estado estado = estadoList.get(i);
estadoView.txtnombreMostrar.setText(estado.getStateName());



estadoView.txtnombreMostrar.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Invernadero.class);
v.getContext().startActivity(intent);
} });


}

关于java - 单击recyclerView新 Activity 尝试调用虚拟方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61448412/

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