簡體   English   中英

Android無法從onPostExecute更新TextView

[英]Android can't update TextView from onPostExecute

我無法在AsyncTask(MainActivity innerclass)中的onPostExecute中設置TextView中的文本

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"); 

                ...
                ...
                ...

        }
    }

}

我已經檢查了其他類似問題的帖子,但是他們在AsyncTask中有TextView定義,在我的情況下我無法理解有什么問題。

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

更改:

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

至:

SQLAsync dlTask = new SQLAsync(url);

代替

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

初始化如下:

SQLAsync dlTask = new SQLAsync(url);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM