2

This might help someone also: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

I am calling method from another class in onPostExecute().

I assume that onPostExecute() is called after doInBackground(String... params) and that is right, according to documentation and debugger.

Calling the method:

protected void onPostExecute(String result) {
    CreateHangOut crtHO = new CreateHangOut();
    crtHO.createHangOut(result);
}

Part of method called, causing NPE (first line of method):

public void createHangOut(String location) {
    String city=autocompleteTV.getText().toString();
   }

Autocomplete TextView(autocompleteTV) is initialized onCreate of the activity.

Here is how I call AsyncTask:

create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new 
HTTPRequest().execute((autocompleteTV.getText()).toString());
            }
    });

Method called onCreate (of activity from where button is clicked) :

private void initialize() {
    gAPI= new GoogleAPIAutocomplete();
    autocompleteTV = (AutoCompleteTextView) 
    findViewById(R.id.crtHOLocOptionsTV);
    setUpAutocomplete();
    create = (Button) findViewById(R.id.crtHOCreateBtn);
    name =(EditText) findViewById(R.id.crtHONameET);
    create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new 
    HTTPRequest().execute((autocompleteTV.getText()).toString());
            }
    });
}
Community
  • 1
  • 1
Ondrej Tokar
  • 4,898
  • 8
  • 53
  • 103
  • Probably `autocompleteTV` is `null`. How you are accessing `autocompleteTV` in `CreateHangOut` class? – ρяσѕρєя K Jan 16 '15 at 18:03
  • Does `autocompleteTv` have any text in it? – Chris Margonis Jan 16 '15 at 18:06
  • I think you are right, it might be null because autocompleteTV is initialized only onCreate, but why is it losing its initialization when I call AsyncTask? Please answer me this and I will mark it as right. – Ondrej Tokar Jan 16 '15 at 18:06
  • @OndrejTokar : `why is it losing its initialization when I call AsyncTask` depend on how you are accessing in `CreateHangOut` – ρяσѕρєя K Jan 16 '15 at 18:08
  • `public void createHangOut(String location) {` is in a separate class from your `Activity`? If so, how do you get a reference to it in that class? Also, do you call `setContentView()` before calling `initialize()`? – codeMagic Jan 16 '15 at 18:10
  • Ahaa so if I access it like this: `CreateHangOut crtHO = new CreateHangOut();` `crtHO.createHangOut(result);` Then it is creating new object so thats why. Please answer it :) Thank you. – Ondrej Tokar Jan 16 '15 at 18:10
  • `createHangOut(String location)` is in the same class as the Activity is. I am just calling it from `AsyncTask` – Ondrej Tokar Jan 16 '15 at 18:12
  • 1
    So `crtHO` is your `Activity`? You don't usually (probably ever) want to initialize an `Activity` that way. [See this answer](http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648) on updating your activity from AsyncTask – codeMagic Jan 16 '15 at 18:22

1 Answers1

1

Because createHangOut method is in CreateHangOut Activity so no need to create new object for accessing method just call it using method name if class which extends AsyncTask class is inner class of CreateHangOut :

protected void onPostExecute(String result) {
    CreateHangOut.this.createHangOut(result);
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • What should I do if that `AsyncTask` is in another class? Should I make method `createHangOut(String result)` static then? But it causes another issues. Any good solution for that matter? – Ondrej Tokar Jan 16 '15 at 18:20
  • @OndrejTokar: if `AsyncTask` is in other class then instead of calling method of Activity send TextView initialized instance to class which is extending `AsyncTask` using class constructor – ρяσѕρєя K Jan 16 '15 at 18:23