rmdan rmdan - 9 months ago 94
Java Question

Attempting to use an incompatible return type - android studio

Can someone help me with this error that I keep getting?
The program that I'm trying to make is online streaming radio.
I need to return the value "value" so i can use it in another java class.

ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,ch);


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

public int onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), mPlayer.class);
i.putExtra("name", ch.get(position));
int value = listView.getItemAtPosition(position);
return value;


The problem occurs in this line:

int value = listView.getItemAtPosition(position);

getItemAtPosition() returns an Object, so you must convert it to an int before assigning it to an int variable. You should do this only if you're sure that your listView contains integers, otherwise you'll get a ClassCastException. Do it like this:

int value = (int)listView.getItemAtPosition(position);

Also, if you startActivity() before using the value anywhere, then it does not make much sense in getting it. Use putExtra to pass it to your next activity if you wish to use it there.