这部分代码: txtView.setText(stringHandler.getString());
txtView.setText(stringHandler.getString());
应该在线程完成后放入。
thread.run(); <------ Start running thread in background TextView txtView = (TextView)this.findViewById(R.id.textView1); txtView.setText(stringHandler.getString()); // the textView was set without knowing whether the Thread is finished or not. // thus, textView is shown as empty, because stringHandler.getString() still has empty string
我的建议是你在里面实现它 asyncTask ,以便您可以在其中设置文本 onPostExecute()
asyncTask
onPostExecute()
祝你好运^^
的 编辑: 强> 这是AsyncTask的一些例子
private class StringSet extends AsyncTask <Void,Void,Void> { @Override protected void doInBackground(Void... params) { try { stringHandler.setString(webRequest()); } catch (Exception e) { stringHandler.setString(e.getMessage()); } } @Override protected void onPostExecute(Void... results) { txtView.setText(stringHandler.getString()); } }
并替换你的 thread.run() 同 new StringSet().execute();
thread.run()
new StringSet().execute();