Search code examples
javaandroidandroid-toast

No suitable method found for makeText


I seem to be getting this error, I'm new to Android Studio and I'm trying to get names from an array into a ListView then when user taps on the any of the list a Toast is activated. But I seem to be stuck with this error below:

error: no suitable method found for makeText(MainActivity,Object,int)
method Toast.makeText(Context,CharSequence,int) is not applicable
(argument mismatch; Object cannot be converted to CharSequence)
method Toast.makeText(Context,int,int) is not applicable
(argument mismatch; Object cannot be converted to int)

Here's my code:

final ArrayList names = new ArrayList();
names.add("Samuel");
names.add("Manuel");
names.add("King");

listv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(MainActivity.this, names.get(position), Toast.LENGTH_LONG).show();
    }
});

What is the problem and how can I fix this?


Solution

  • Change to:

    Toast.makeText(MainActivity.this, String.valueOf(names.get(position)), Toast.LENGTH_LONG).show();