3

I can't seem to call a AsyncTask from a static method. It says "cannot be referenced from a static context". It is important that this method IS static and I need to have it like that for a few other processes of mine.

Is there a way to call the AsyncTask from the method?

public static void UpdateResults(String requestSearch){
    new GetSearchResults(requestSearch).execute(); //shows an error
}

class GetSearchResults extends AsyncTask<Void, Void, Void> {

    String requestSearch;

    GetSearchResults(String searchtext){
        this.requestSearch = searchtext;
    }

    @Override
    protected Void doInBackground(Void... params) {
        //functions continuing 
    }
}

EDIT: Anands solution worked however it threw this exception as soon as it got to the method:

FATAL EXCEPTION: AsyncTask #1
    Process: com.eproject.eproject.emobile, PID: 26831
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.NullPointerException
            at com.eproject.eproject.emobile.SearchTabs.SearchPeopleTab$GetSearchResults.doInBackground(SearchPeopleTab.java:78)
            at com.eproject.eproject.emobile.SearchTabs.SearchPeopleTab$GetSearchResults.doInBackground(SearchPeopleTab.java:63)

The line 78 which shows null pointer, points to this line of code:

SharedPreferences accPref = getActivity().getSharedPreferences(
                    "accPref", Context.MODE_PRIVATE);

Looks like it can't get hold of the SharedPreferences from the AsyncTask method now. It worked well before. What is wrong with it?

Danielson
  • 2,605
  • 2
  • 28
  • 51
Jay
  • 4,873
  • 7
  • 72
  • 137

3 Answers3

6

If Async class is a non-static inner class inside of your Activity, then you need an instance of the enclosing class in order to instantiate the inner class.

You have to call like this way inside static method:

SearchPeopleTab outerClass = new SearchPeopleTab(); //Outer class
        GetSearchResults task = outerClass.new GetSearchResults(requestSearch);
        task.execute();
Anand Singh
  • 5,672
  • 2
  • 23
  • 33
  • some value is `null` in your doInBackground method. Can you post code of your do in background? (Line 78) @Earthling – Anand Singh Jul 27 '15 at 06:01
  • Although this answers the question, the code is very unconventional. – Hindol Jul 27 '15 at 06:21
  • What's the conventional way to do it @Hindol? I'm open for more ideas – Jay Jul 27 '15 at 06:25
  • I have one idea, but it may not be the best. Since you need access to context (*Activity* **is** *Context*) from a static method, you can use the Application Context. See this [SO question](http://stackoverflow.com/questions/987072/using-application-context-everywhere). Then you need not access the *outer class* from the *inner class*, which is a bad practice IMO. You cannot then move the inner class to a separate file easily. – Hindol Jul 27 '15 at 06:35
4

Declare your inner class as static:

static class GetSearchResults extends AsyncTask<Void, Void, Void> {

Or move it into its own file.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • I don't know why you got the downvote but this almost does the trick. It's just that the application's context cannot be referenced from a static method now. For example: SharedPreferences accpref = getActivity().getSharedPreferences( "accpref", Context.MODE_PRIVATE); – Jay Jul 27 '15 at 05:42
  • In terms of your posted code, this answer is correct. If you are trying to reference member variables or methods of the outer class in the inner class (not shown in question), you have a problem: you need an instance of the outer class. Impossible to say how to make this change without more information. – Andy Turner Jul 27 '15 at 05:47
  • Quite simply, I'm trying to get the application context inside the GetSearchResults AsyncTask but since I changed it to static, it won't let me refer the getActivity() context now, mate – Jay Jul 27 '15 at 05:49
  • If you want to call an instance method on a class, you need an instance of the class. You don't have this in a static context. – Andy Turner Jul 27 '15 at 05:52
0

your method UpdateResults is static,but inner class GetSearchResults is not defined static,you can not involve none static class in static method,because none class is belong to object,but in static method there is no ogject

simonws
  • 65
  • 5