使用onProgressUpdate从Async Task更新UI中的文本

时间:2013-12-27 13:17:15

标签: android

我正在使用AsyncTask来执行http请求。但是在尝试更新UI元素时代码崩溃了。

class ServerRequest extends AsyncTask<String,String,String> {
    private  View rootView;
    private Activity rootAct;
    private String result;
    public ServerRequest(View view,Activity act) {
        // TODO Auto-generated constructor stub\
        rootView = view;
        rootAct = act;
    }
    protected String doInBackground(String... params) {
        String URL=params[0];
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString="DEFAULT";

        try {
            response = httpclient.execute(new HttpGet(URL));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();

                //..more logic
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        result = responseString;
        publishProgress(responseString);
        return  responseString;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        EditText tt = (EditText) rootView.findViewById(R.id.codeResult);
        tt.setText("Sample Text"); //Causing APP TO CRASH
    }
    protected void onPostExecute(String result) {
        Log.d("retval",result);
    }
}

setText中的onProgressUpdate导致我的应用程序崩溃。我已经完成了大部分线程,但找不到解决方案。

LOG

12-27 18:32:00.163: E/AndroidRuntime(24685):    at com.trueeduc.qrcode.ServerRequest.onProgressUpdate(ServerRequest.java:1)
12-27 18:32:00.163: E/AndroidRuntime(24685):    at com.trueeduc.qrcode.ServerRequest.onProgressUpdate(ServerRequest.java:69)
12-27 18:32:00.163: E/AndroidRuntime(24685):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
12-27 18:32:00.163: E/AndroidRuntime(24685):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 18:32:23.253: E/AndroidRuntime(24801):    at com.trueeduc.qrcode.ServerRequest.onProgressUpdate(ServerRequest.java:69)
12-27 18:32:23.253: E/AndroidRuntime(24801):    at com.trueeduc.qrcode.ServerRequest.onProgressUpdate(ServerRequest.java:1)
12-27 18:32:23.253: E/AndroidRuntime(24801):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
12-27 18:35:00.233: E/AndroidRuntime(24888):    at com.trueeduc.qrcode.ServerRequest.onProgressUpdate(ServerRequest.java:69)
12-27 18:35:00.233: E/AndroidRuntime(24888):    at com.trueeduc.qrcode.ServerRequest.onProgressUpdate(ServerRequest.java:1)
12-27 18:35:00.233: E/AndroidRuntime(24888):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
12-27 18:35:00.233: E/AndroidRuntime(24888):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 19:02:05.643: E/AndroidRuntime(25128):    at com.trueeduc.qrcode.ServerRequest.onProgressUpdate(ServerRequest.java:69)
12-27 19:02:05.643: E/AndroidRuntime(25128):    at com.trueeduc.qrcode.ServerRequest.onProgressUpdate(ServerRequest.java:1)
12-27 19:02:05.643: E/AndroidRuntime(25128):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
12-27 19:02:05.643: E/AndroidRuntime(25128):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 19:02:22.373: E/AndroidRuntime(25192):    at com.trueeduc.qrcode.ServerRequest.onProgressUpdate(ServerRequest.java:69)
12-27 19:02:22.373: E/AndroidRuntime(25192):    at com.trueeduc.qrcode.ServerRequest.onProgressUpdate(ServerRequest.java:1)
12-27 19:02:22.373: E/AndroidRuntime(25192):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
12-27 19:02:22.373: E/AndroidRuntime(25192):    at android.os.Handler.dispatchMessage(Handler.java:99)

2 个答案:

答案 0 :(得分:4)

我认为是因为您没有引用EditText要在AsyncTask中使用class ServerRequest extends AsyncTask<String,String,String> { private View rootView; private Activity rootAct; private String result; public ServerRequest(View view,Activity act) { // TODO Auto-generated constructor stub\ rootView = view; rootAct = act; } protected String doInBackground(String... params) { String URL=params[0]; HttpClient httpclient = new DefaultHttpClient(); HttpResponse response; String responseString="DEFAULT"; try { response = httpclient.execute(new HttpGet(URL)); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); responseString = out.toString(); //..more logic } else{ //Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } result = responseString; publishProgress(responseString); return responseString; } @Override protected void onProgressUpdate(String... values) { // TODO Auto-generated method stub super.onProgressUpdate(values); //updated this line EditText tt = (EditText) act.findViewById(R.id.codeResult); tt.setText("Sample Text"); //Causing APP TO CRASH } protected void onPostExecute(String result) { Log.d("retval",result); } } 的活动,因此我对您的代码进行了一些更改,并使用以下代码更新代码:

{{1}}

答案 1 :(得分:0)

EditText对象为null。之后它开始工作了。感谢您指出解决问题的指南