java - 在recyclerview中单击一项以阅读详细信息时出现NullPointerException
问题描述
我正在尝试创建一个简单的食谱应用程序。单击该项目时将显示每个配方的详细信息的回收视图。当我单击该项目时,应用程序崩溃并显示 nullPointerException。我不知道如何纠正它?
主要活动
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecipeAdapter adapter;
List<Recipe> recipeList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recipeRecyclerView);
recipeList = new ArrayList<>();
recipeList.add(new Recipe(
"Good-Fat Fish",
"1 spoon sauce\n" + "fish\n" + "lemon",
"Put the fish in pot and steam it with sauce and lemon",
"One serving",
"Healthy and improve memory"));
recipeList.add(new Recipe(
"Good-Fat pork",
"1 spoon sauce\n" + "fish\n" + "lemon",
"Put the fish in pot and steam it with sauce and lemon",
"One serving",
"Healthy and improve memory"));
adapter = new RecipeAdapter(this, recipeList);
recyclerView.setLayoutManager(new LinearLayoutManager(this)); //or new GridLayoutManager(this, 1)
recyclerView.setAdapter(adapter);
}
}
适配器
public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.RecipeHolder> {
private Context context;
private List<Recipe> recipeList;
public RecipeAdapter(Context context, List<Recipe> recipeList) {
this.context = context;
this.recipeList = recipeList;
}
@NonNull
@Override
public RecipeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.recipe_single_cardview, parent, false);
return new RecipeHolder(view);
// or return new RecipeHolder(LayoutInflater.from(context).inflate(R.layout.recipe_single_cardview, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecipeHolder holder, int position) {
Recipe recipe = recipeList.get(position);
holder.showRecipeName.setText(recipe.getRecipeName());
holder.showInfo.setText(recipe.getInfo());
}
@Override
public int getItemCount() {
return recipeList.size();
}
public class RecipeHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView showRecipeName, showInfo;
public RecipeHolder(@NonNull View itemView) {
super(itemView);
showRecipeName = (TextView) itemView.findViewById(R.id.cardView_recipeName);
showInfo = (TextView) itemView.findViewById(R.id.cardView_info);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Recipe recipe = recipeList.get(getAdapterPosition());
Intent intent = new Intent(context, ReadActivity.class);
intent.putExtra("recipe", recipe); //need to implements Serializable at Recipe class
context.startActivity(intent);
}
}
}
食谱类
public class Recipe implements Serializable {
private String recipeName, ingredients, preparation, yield, info;
public Recipe() {
}
public Recipe(String recipeName, String ingredients, String preparation, String yield, String info) {
this.recipeName = recipeName;
this.ingredients = ingredients;
this.preparation = preparation;
this.yield = yield;
this.info = info;
}
public String getRecipeName() {
return recipeName;
}
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
public String getPreparation() {
return preparation;
}
public void setPreparation(String preparation) {
this.preparation = preparation;
}
public String getYield() {
return yield;
}
public void setYield(String yield) {
this.yield = yield;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
阅读活动
public class ReadActivity extends AppCompatActivity {
private TextView recipeNameTV, ingredientsTV, preparationTV, yieldTV, infoTV;
private String name, ingredients, preparation, yield, info;
private Recipe recipe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read);
recipeNameTV = (TextView) findViewById(R.id.cardView_recipeName);
ingredientsTV = (TextView) findViewById(R.id.read_ingredients);
preparationTV = (TextView) findViewById(R.id.read_preparation);
yieldTV = (TextView) findViewById(R.id.read_yield);
infoTV = (TextView) findViewById(R.id.read_info);
recipe = (Recipe) getIntent().getSerializableExtra("recipe");
Intent intent = getIntent();
name = intent.getExtras().getString("recipeName");
ingredients = intent.getExtras().getString("ingredients");
preparation = intent.getExtras().getString("preparation");
yield = intent.getExtras().getString("yield");
info = intent.getExtras().getString("info");
recipeNameTV.setText(name);
ingredientsTV.setText(ingredients);
preparationTV.setText(preparation);
yieldTV.setText(yield);
infoTV.setText(info);
}
}
错误:
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.amethyst.mindrecipe, PID: 26121
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amethyst.mindrecipe/com.amethyst.mindrecipe.ReadActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3146)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3296)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:114)
Process 26121 terminated.
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:74)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
at android.os.Handler.dispatchMessage(Handler.java:106)
感谢您在高级的帮助:)
解决方案
这是一个粗心的错误,(使用错误的ID设置项目)
我变了
recipeNameTV = (TextView) findViewById(R.id.cardView_recipeName);
至
recipeNameTV = (TextView) findViewById(R.id.read_recipeName);
谢谢!
推荐阅读
- python - 在 Python 中像记事本一样增加 IDE 的容量?
- r - 在 proj4string(shp) 中:CRS 对象有注释,在输出中丢失
- react-admin - 为什么react-admin在删除后,在被删除的对象上,在乐观渲染模式下调用getOne?
- javascript - Nodejs - 捕获由包含嵌套异步获取的函数返回的对象数组
- javascript - (React Frontend) 后端收到数据时,无法访问返回的对象
- android - 我想要自定义 UI 与破折线相同的票
- firebase - 如何使用 uid 文档 ID FirebaseAuth Firestore 获取 Firestore 字段信息
- javascript - RegEx l33t 说话识别器?
- r - 使用相同数据集进行预测时,预测和寓言包返回的参数略有不同
- java - 如何在没有参数的情况下为对象设置值