'constructor in class cannot be applied to given types android studio

i am making a recipe app and i m getting this error : constructor HomeItem in class HomeItem cannot be applied to given types; recipe.add(new HomeItem("viay",R.drawable.vietnamese));

This is my code:

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));
}

}

And here is 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;
}

}

What did i declared wrong in HomeItem? Thanks



Solution 1:[1]

You cannot pass arguments to a constructor with no arguments.

Try this

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

p.s. use camel case for variables and feilds

Solution 2:[2]

It's a simple coding problem. You define a constructor in HomeItem class without any parameters/arguments, but you try to instantiate HomeItem object and supplies some arguments to its constructor. You should use your setters method instead, or refactor your code to initialize all HomeItem class properties/fields in your constructor.

Hope this helps.

Solution 3:[3]

Well you are calling a constructor with parameters, but you only have an empty constructor, try this:

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tag Howard
Solution 2 FEBRYAN ASA PERDANA
Solution 3 Hasan Bou Taam