title: A method doesn't work when I use Button.setText(" ") inside it
body: <p>I have an android app that consists of two classes JSONTransmitter class and MainActivity class the goal of this app is to post and get response from a php file and display this response on a widget.</p>
  
  <p>I have a small problem on  onResponse method of MainActivity class-when I use button.setText("") it doesn't work if not it works! </p>
  
  <p>JSONTransmitter class</p>
  
  <pre><code>public class JSONTransmitter extends AsyncTask&lt;JSONObject, Void, String&gt;
  {
  HttpResponse response; 
  String url = "http://192.168.1.13:89/Derdeery/b.php";
  private AsyncCallback asyncCallback;
  public static interface AsyncCallback {
  void onResponse(String res);
  
  }
  public JSONTransmitter(Context context) 
  {
      // attach the callback to any context
      asyncCallback = (AsyncCallback) context;
  }
  protected String doInBackground(JSONObject... data) { 
       JSONObject json = data[0]; 
  
       HttpClient client = new DefaultHttpClient(); 
       HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000); 
       StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build()); 
       JSONObject jsonResponse = null; 
  
       HttpPost post = new HttpPost(url); 
  
       // initialize a return string 
       String resFromServer = ""; 
  
       try { 
       StringEntity se = new StringEntity("json="+json.toString()); 
  
       post.addHeader("content-type", "application/x-www-form-urlencoded"); 
       post.setEntity(se); 
  
       HttpResponse response; 
       response = client.execute(post); 
  
       // when you can get the json, you modify the return string 
       resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity()); 
  
       Log.i("Response from server", resFromServer); 
  
  
       } 
       catch (Exception e) 
       { e.printStackTrace();} 
  
       // now you can return a string 
       return resFromServer; 
  
  }
  </code></pre>
  
  <p>MainActivity class</p>
  
  <pre><code>public class MainActivity extends Activity implements JSONTransmitter.AsyncCallback {
  
  
      private Button b ; 
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
       Button   b = (Button)findViewById(R.id.button1) ;  
  
          b.setOnClickListener(new OnClickListener() { 
               @Override 
               public void onClick(View view) { 
                  mymethod() ;
  
  
                  } 
               });      
      }
  
      public void mymethod()
      {
           try {
  
              TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
              String  mPhoneNumber = tm.getLine1Number();  
  
               JSONObject toSend = new JSONObject();
               toSend.put("msg", mPhoneNumber );
  
  
  
               new JSONTransmitter(this).execute(new JSONObject[] {toSend});
  
           } catch (JSONException e) {
               e.printStackTrace();
           }
  
  
  
  
      }
  
      @Override
      public  void onResponse(String res) 
      {
          // TODO Auto-generated method stub
          Log.v("From_MainActivity", "AsyncTask returns this: "+res);
              b.setText(res);
      }
  
  
     }
  </code></pre>
  
  <p>xml file</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity" &gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:text="Button" /&gt;
  
  
  &lt;/RelativeLayout&gt; 
  </code></pre>
  
  <p>After updating JSONTransmitter class to the following</p>
  
  <pre><code>public class JSONTransmitter extends AsyncTask&lt;JSONObject, Void, String&gt; {
  HttpResponse response; 
  String url = "http://192.168.1.20:89/Derdeery/b.php";
  private AsyncCallback asyncCallback;
  
  public static interface AsyncCallback {
      void onResponse(String res);
  
  }
  public JSONTransmitter(Context context) 
  {
      // attach the callback to any context
      asyncCallback = (AsyncCallback) context;
  }
  @Override
  protected void onPostExecute(String result) {
    super.onPostExecute(result);
    asyncCallback.onResponse(result);
  }
  protected String doInBackground(JSONObject... data) { 
       JSONObject json = data[0]; 
  
       HttpClient client = new DefaultHttpClient(); 
       HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000); 
       StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build()); 
       JSONObject jsonResponse = null; 
  
       HttpPost post = new HttpPost(url); 
  
       // initialize a return string 
       String resFromServer = ""; 
  
       try { 
       StringEntity se = new StringEntity("json="+json.toString()); 
  
       post.addHeader("content-type", "application/x-www-form-urlencoded"); 
       post.setEntity(se); 
  
       HttpResponse response; 
       response = client.execute(post); 
  
       // when you can get the json, you modify the return string 
       resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity()); 
  
       Log.i("Response from server", resFromServer); 
  
  
       } 
       catch (Exception e) 
       { e.printStackTrace();} 
  
       // now you can return a string 
       return resFromServer; 
  
  } 
  </code></pre>
  
  <p>my app crashed and I get this log errors </p>
  
  <pre><code> 01-18 06:30:07.721: E/AndroidRuntime(810): FATAL EXCEPTION: main
  01-18 06:30:07.721: E/AndroidRuntime(810): java.lang.NullPointerException
  01-18 06:30:07.721: E/AndroidRuntime(810):  at com.coffeecodes.coffecodesdoodle.MainActivity.onResponse(MainActivity.java:66)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at com.coffeecodes.coffecodesdoodle.JSONTransmitter.onPostExecute(JSONTransmitter.java:48)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at com.coffeecodes.coffecodesdoodle.JSONTransmitter.onPostExecute(JSONTransmitter.java:1)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at android.os.AsyncTask.finish(AsyncTask.java:631)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at android.os.Handler.dispatchMessage(Handler.java:99)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at android.os.Looper.loop(Looper.java:137)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at android.app.ActivityThread.main(ActivityThread.java:5039)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at java.lang.reflect.Method.invokeNative(Native Method)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at java.lang.reflect.Method.invoke(Method.java:511)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  01-18 06:30:07.721: E/AndroidRuntime(810):  at dalvik.system.NativeStart.main(Native Method)
  01-18 06:30:12.950: I/Process(810): Sending signal. PID: 810 SIG: 9
  </code></pre>