AsyncTask中的子字符串导致app强制退出

时间:2015-06-25 10:35:03

标签: android android-asynctask

我有这段代码:

package com.jamie.translate;

import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.net.URI;

/**
 * Created by Jamie on 25/6/2015.
 */
public class TranslateTextTask extends AsyncTask<TextView, Void, String> {
    TextView t;
    String result = "";
    String sourceText = "";
    String sourceLanguage = "";
    String resultLanguage = "";

    public TranslateTextTask(String _sourceText, String _sourceLanguage, String _resultLanguage) {
        this.sourceText = _sourceText;
        this.sourceLanguage = _sourceLanguage;
        this.resultLanguage = _resultLanguage;
    }

    protected void d(String tag, String errorMessage) {
        MainActivity.showNormalMessage(MainActivity.applicationContext, errorMessage);
    }

    @Override
    protected String doInBackground(TextView... params) {
        this.t = params[0];
        return GetTranslatedText();
    }

    final String GetTranslatedText() {
        Uri.Builder builder = new Uri.Builder();
        String langPair;
        URI uri = null;
        final HttpClient httpClient;
        final HttpGet request;
        final HttpResponse[] response = new HttpResponse[1];
        final BufferedReader reader = null;
        final String JSONResult = "";
        String translatedString;

        if(sourceText.length() > 500) {
            MainActivity.showErrorMessage(MainActivity.applicationContext, "Source text can only be max. 500 characters");
            return "";
        }

        d("Test", "Tag2");try {
            String x = ("en - English").substring(0, 2);
            String y = resultLanguage.substring(0, 2);

            langPair =
                    sourceLanguage.substring(0, 2)
                            + "|"
                            + resultLanguage.substring(0, 2);
        } catch (Exception e) {
            MainActivity.showErrorMessage(MainActivity.applicationContext, "Source language or result language invalid.");
            return "";
        }

        d("Test", "Tag3");try {
            uri = new URI(builder.scheme("http")
                    .authority("api.mymemory.translated.net")
                    .appendPath("get")
                    .appendQueryParameter("langpair", langPair)
                    .appendQueryParameter("q", sourceText)
                    .build()
                    .toString());
        } catch (Exception e) {
            MainActivity.showErrorMessage(MainActivity.applicationContext, "Failed to build HTTP request URL.");
            return "";
        }

        d("Test", "Tag4");try {
            httpClient = new DefaultHttpClient();
            request = new HttpGet();
            request.setURI(uri);
        } catch (Exception e) {
            MainActivity.showErrorMessage(MainActivity.applicationContext, "Failed to build HTTP request.");
            return "";
        }

        d("Test", "Tag5");try {
            // response = httpClient.execute(request);
            d("Test", "Tag6");try {
                response[0] = (httpClient.execute(request));
                return MainActivity.handleTranslateRequest(MainActivity.applicationContext, response[0]);
            } catch (Exception e) {
                MainActivity.showErrorMessage(MainActivity.applicationContext, e.toString());
                return "";
            }
        } catch (Exception e) {
            MainActivity.showErrorMessage(MainActivity.applicationContext, e.toString());
            // MainActivity.showErrorMessage(MainActivity.applicationContext, "Failed to complete HTTP request.");
            return "";
        }
    }

    protected void onPostExecute(String result) {
        t.setText(result);
    }
}

我开始使用以下代码:

new TranslateTextTask(((EditText) findViewById(R.id.source)).getText(), translateSourceLanguage, translateResultLanguage).execute((EditText) findViewById(R.id.result));

调试过程中变量值的示例值包括

((EditText) findViewById(R.id.source)).getText(): "Hi"
translateSourceLanguage: "en - English"
translateResultLanguage: "it - Italiano"

没有互联网连接问题。 在调试过程中,我设置了足够的断点,发现强行退出问题发生在这一行:

String x = ("en - English").substring(0, 2);

我该怎么做才能解决这个问题?这似乎是一个非常简单的问题,但根本无法阻止它。

修改

LogCat消息:

06-25 18:41:04.318    9337-9534/com.jamie.translate E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.jamie.translate, PID: 9337
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
            at android.os.Handler.<init>(Handler.java:200)
            at android.os.Handler.<init>(Handler.java:114)
            at android.widget.Toast$TN.<init>(Toast.java:376)
            at android.widget.Toast.<init>(Toast.java:108)
            at android.widget.Toast.makeText(Toast.java:267)
            at com.jamie.translate.MainActivity.showNormalMessage(MainActivity.java:26)
            at com.jamie.translate.TranslateTextTask.d(TranslateTextTask.java:35)
            at com.jamie.translate.TranslateTextTask.GetTranslatedText(TranslateTextTask.java:60)
            at com.jamie.translate.TranslateTextTask.doInBackground(TranslateTextTask.java:41)
            at com.jamie.translate.TranslateTextTask.doInBackground(TranslateTextTask.java:21)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:0)

您不得在doInBackground()上触摸UI(即发布Toasts或更新TextView等)。如果您需要更新用户界面,则需要在onPostExecute()中执行此操作,或者如果适合,则使用onProgressUpdate()