Pregunta

This will probably have an obvious answer to many but I can't seem to figure it out. I am trying to make an api call within an android application, but keep running into "..... cannot be resolved" I have three Java classes, FoodSearchActivity, GetItemsActivity and GetData!

FoodSearchActivity extends Activity; 

This class declares the following

public ListView list;
public Button btnSearch;
public EditText txtFoodSearch;

but in the GetItemsActivity class, txtFoodSearch cannot be resolved unless I extend FoodSearchActivity. Which in turn means I can't implement AsyncTask. When GetItemsActivity does implement AsyncTask I get the error:

 " The type AsyncTask<String,String,String> cannot be a superinterface of GetItemsActivity; a superinterface must be an interface"

GetItemsActivity extends FoodSearchActivity;

To try solve this I created a new class GetData which extends AsyncTask<String, String, String>

GetData extends AsyncTask<String, String, String>

I have no errors when i do this but when I do

txtFoodSearch.getText().toString();

I am back at the txtFoodSearch cannot be resolved. txtFoodSearch is an EditText field which takes user input. How can i change this so txtFoodSearch is visible in the other classes?? It's already declared as public. Any tips or advice would be great. Can post source code if needed!

¿Fue útil?

Solución

Try following the public declaration that you have assigned to txtFoodSearch with the declaration of static. This is because the static keyword makes variables such as txtFoodSearch globally accessible, while their class is loaded.

So, after you declare public static txtFoodSearch in its corresponding class, you should be able to just do: Classname.txtFoodSearch in any other class to access the content of txtFoodSearch. Be certain however that the class containing txtFoodSearch is declared as public.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top