gpt4 book ai didi

java - 类中的构造函数不能应用于给定类型android studio

转载 作者:行者123 更新时间:2023-12-02 08:39:28 25 4
gpt4 key购买 nike

我正在制作一个食谱应用程序,但收到此错误:类 HomeItem 中的构造函数 HomeItem 无法应用于给定类型; Recipe.add(new HomeItem("viay",R.drawable.vietnamese));

这是我的代码:

public class HomeFragment extends Fragment { View v; private RecyclerView myrecyclerview; private List recipe; public HomeFragment() { }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

v = inflater.inflate(R.layout.fragment_home,container,false);

return v; }

public interface OnFragmentInteractionListener {
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

recipe = new ArrayList<>();
recipe.add(new HomeItem("viay",R.drawable.vietnamese));
}

}

这是 HomeItem.java:

public class HomeItem {

private String RecipeName;
private int RecipeImage;

public HomeItem() {

}

public String getRecipeName() {
return RecipeName;
}

public int getRecipeImage() {
return RecipeImage;
}

public void setRecipeName(String recipeName) {
RecipeName = recipeName;
}

public void setRecipeImage(int recipeImage) {
RecipeImage = recipeImage;
}

}

我在 HomeItem 中声明了什么错误?谢谢

最佳答案

不能将参数传递给没有参数的构造函数。

试试这个

public HomeItem(String recipieName, int recipieImage) {
this.recipieName=recipieName;
this.recipieImage=recipieImage;
}

附:对变量和字段使用驼峰式大小写

关于java - 类中的构造函数不能应用于给定类型android studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61468979/

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