2016-02-03 32 views
3
更新的TextView

我不能從裏面的AsyncTask的onPostExecute(MainActivity內部類)的Android無法從onPostExecute

public class MainActivity extends Activity { 
... 
private TextView txt_msg; 
... 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    txt_msg = (TextView) findViewById(R.id.txt_msg); 
    txt_msg.setText("ON create"); // <-- OK this works 

    ... 
    ... 

    btn = (Button) findViewById(R.id.btn_associa); 
    btn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
        String url = "http://myurl.php?a=1&b=2&c=3"; 
        MainActivity.SQLAsync dlTask = new MainActivity().new SQLAsync(url); 
        dlTask.execute(); 
      } 
     } 
    }); 

    ... 
    ... 
    ... 


    class SQLAsync extends AsyncTask<URL, Integer, String> { 
    private String myurl = ""; 

     public SQLAsync(String _url) { 
      super(); 
      myurl = _url; 
     } 

     @Override 
     protected void onPreExecute() { 
     //empty 
     } 

     @Override 
     protected String doInBackground(URL... urls) { 
      InputStream is = null; 
      String result = ""; 
      JSONObject jArray = null; 

      try { 
       URL url = new URL(myurl); 
       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       conn.setRequestMethod("GET"); 
       int resp_code = conn.getResponseCode(); 
       is = new BufferedInputStream(conn.getInputStream()); 

      } catch (Exception e) { 
       Log.e("tag", "Error in http connection " + e.toString()); 
      } 

      try { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while (reader.readLine() != null) { 
        line = reader.readLine(); 
        sb.append(line + "\n"); 
       } 
       is.close(); 
       result = sb.toString(); 


      } catch (Exception e) { 
       Log.e("tag", "Error converting result " + e.toString()); 
      } 
      return result; 
     } 

     protected void onProgressUpdate(Integer... progress) { 
     //empty 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      String result = s; 
      Log.i("davide", "String post execute " + s.toString()); 

      if (txt_msg!=null) // <-- THIS IS NULL 
       txt_msg.setText("MESSAGGIO DI SISTEMA"); 

       ... 
       ... 
       ... 

     } 
    } 

} 

我檢查了其他職位類似問題設置文本中的TextView,但他們有AsyncTask裏面的TextView定義,在我的情況下,我不明白什麼是錯的。

+0

因爲你沒有初始化textview對象它總是會給你null ...我想你應該在設置文本之前檢查'String'是否爲null – Mohit

回答

1

您正在從MainActivity類的新實例創建新的SQLAsync,而不是現有的新類。

變化:

MainActivity.SQLAsync dlTask = new MainActivity().new SQLAsync(url); 

要:

SQLAsync dlTask = new SQLAsync(url); 
+1

爲什麼不只是:SQLAsync dlTask​​ = new SQLAsync(url)? –

+0

@JawadLeWywadi他真的應該這樣做,所以我會編輯答案謝謝。 –

+0

謝謝!這解決了我的問題。這和Clairvoyant的答案都是正確的。我很抱歉只能選擇一個:) – user3854399

1

而不是

MainActivity.SQLAsync dlTask = new MainActivity().new SQLAsync(url); 

初始化它像如下:

SQLAsync dlTask = new SQLAsync(url);