Esat IBIS
Esat IBIS

Reputation: 374

Android AsyncTask error in declaration

I'm getting this error "No enclosing instance of type Datagetter is accessible. Must qualify the allocation with an enclosing instance of type Datagetter (e.g. x.new A() where x is an instance of Datagetter)." and my code is

public static void initializeValues
    (String _NAMESPACE , String _URL , String _SOAP_ACTION , 
String _METHOD_NAME , String _PARAM_NAME , String _PARAM_VALUE)

    {
        NAMESPACE = _NAMESPACE ;
        URL = _URL ;
        SOAP_ACTION = _SOAP_ACTION ;
        METHOD_NAME = _METHOD_NAME ;
        PARAM_NAME = _PARAM_NAME ;
        PARAM_VALUE = _PARAM_VALUE ;
        TAG = "Name of log" ;


        AsyncCallWS task = new AsyncCallWS();
        //Call execute
         task.execute();

    }

AsyncCallWS

public class AsyncCallWS extends AsyncTask<String, Void, Void> {

        protected Void doInBackground(String... params) {
            Log.i(TAG, "doInBackground");
            getDataFromWebservice();
            return null;
        }


        protected void onPostExecute(Void result) {
            Log.i(TAG, "onPostExecute");
        //    tv.setText(fahren + "° F");
        }


        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
         //   tv.setText("Calculating...");
        }


        protected void onProgressUpdate(Void... values) {
            Log.i(TAG, "onProgressUpdate");
        }

    }

Upvotes: 1

Views: 607

Answers (3)

soxfmr
soxfmr

Reputation: 58

Because you creating the new object in "static" method. You should add the modifier "static" to AsyncCallWS class such as :

public static class AsyncCallWS extends AsyncTask<String, Void, Void> { ...... } 

Upvotes: 1

laalto
laalto

Reputation: 152807

The method where you are instantiating the asynctask is static. However, the AsyncCallWS seems to be a non-static inner class. Non-static inner classes hold a reference to the parent and therefore cannot be accessed without the parent object.

Probably your async task should be declared static - most often having an asynctask non-static is a programming error.

Upvotes: 6

ravindra.kamble
ravindra.kamble

Reputation: 1023

Change your method signature

public static void initializeValues()

to

public void initializeValues()

Upvotes: 0

Related Questions