title: Wait for AsyncTask to finish
body: <p>Hi I'm making Login page that access MySQL database. But my <code>Activity</code> always runs the code that check fail/success before it finishes the <code>AsyncTask</code>.</p>
  
  <p>I tried using <code>asynctask.get()</code> method, but it just freeze my UI and doesn't work.</p>
  
  <p>I tried this <a href="https://stackoverflow.com/a/6396376/1318622">answer</a> that said I should call the result-checker method inside <code>onPostExecute()</code>. </p>
  
  <p>But since I need to change the TextView to show success/failed, it results in <code>NullPointerException</code> because I instantiate the TextView inside <code>onCreate()</code>.</p>
  
  <p>I can't move the TextView instantiation into constructor because it will return <code>NullPointerException unable to instantiate activity ComponentInfo</code>.</p>
  
  <p><strong>Login.java</strong></p>
  
  <pre><code>public class Login extends Activity{
  
      //declare global Views here
  
      protected void onCreate(Bundle bundle){
          //Setup views
      }
  
      protected void onClick(View v){
          //Setup necessary variables
          AsyncClass async = new AsyncClass(this);
          async.execute(username, password);
      }
  
      public void checkSuccess(boolean success){
          if(success)
              textView1.setText("Success");
          else
              textView1.setText("Failed");
      }
  }
  </code></pre>
  
  <p><strong>AsyncClass.java</strong></p>
  
  <pre><code>public class AsyncClass extends AsyncTask&lt;String, String, JSONObject&gt;{
      protected JSONObject doInBackground(String... params){
          //access database
      }
  
      protected void onPostExecute(JSONObject json){
          //read the json result
          Login login = new Login();
          login.checkSuccess(true);
      }
  }
  </code></pre>
  
  <p>Any solution? Thanks</p>