【问题标题】:Android can't update TextView from onPostExecuteAndroid 无法从 onPostExecute 更新 TextView
【发布时间】:2016-02-03 16:48:25
【问题描述】:

我无法通过 AsyncTask(MainActivity 内部类)内的 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 定义,就我而言,我不明白哪里出了问题。

【问题讨论】:

  • 由于您没有初始化 textview 对象,它总是会给您 null ...我认为您应该在设置文本之前检查 String 是否为 null

标签: java android android-asynctask textview


【解决方案1】:

您正在从MainActivity 类的新实例而非现有实例创建新的SQLAsync

变化:

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

收件人:

SQLAsync dlTask = new SQLAsync(url);

【讨论】:

  • 为什么不只是:SQLAsync dlTask​​ = new SQLAsync(url)?
  • @JawadLeWywadi 他真的应该这样做,所以我会编辑答案谢谢。
  • 谢谢!这解决了我的问题。这和千里眼的答案都是正确的。很抱歉只能选择一个:)
【解决方案2】:

代替

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

如下初始化:

SQLAsync dlTask = new SQLAsync(url);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多