AsyncTask:java.lang.ClassCastException:java.lang.Object []无法强制转换为java.lang.String []

时间:2015-04-24 15:23:50

标签: android android-asynctask

你能帮我解决这个问题吗? 它返回:“引起:java.lang.ClassCastException:java.lang.Object []无法强制转换为java.lang.String []”并且消息指向行号,其中声明了类“MyAsyncTask”(这段代码的第一行)。

private class MyAsyncTask extends AsyncTask<String, String, String> {
    final GetHeader a = new GetHeader();
    final MainActivity c = new MainActivity(); 
    int count = 0;

    protected void onProgressUpdate(String... title) {

        c.Header.setText("Text: " + title);
    }

    protected void onPostExecute(String... string) {

        c.Header.setText("Mission Complete");

    }

    @Override
    protected String doInBackground(String... string) {
        boolean stopCommand = false;
        String title = "";

        do {
            count++;
            try {

                title = "some title";
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                wait(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        while (stopCommand != true);

        return title;
    }
}

logcat返回:

> 1709-1735/myapps.myApp E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
>     Process: myapps.myApp, PID: 1709
>     java.lang.RuntimeException: An error occured while executing doInBackground()
>             at android.os.AsyncTask$3.done(AsyncTask.java:304)
>             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:818)
>      Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]
>             at myapps.myApp.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:98)
>             at android.os.AsyncTask$2.call(AsyncTask.java:292)
>             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:818)

这是此活动的推出方式:

public void onClick(View view) throws IOException, InterruptedException {

        AsyncTask myTask = new MyAsyncTask();
myTask.execute();

4 个答案:

答案 0 :(得分:0)

onProgressUpdate

中将标题更改为标题[0]
protected void onProgressUpdate(String... title) {

    c.Header.setText("Text: " + title[0]);
}

onProgressUpdate接受表示进度的值。

答案 1 :(得分:0)

protected void onPostExecute(String... string) {
                                   ^^^

应该是:

protected void onPostExecute(String string) {

要防止此类错误,请始终使用@Override

答案 2 :(得分:0)

删除它,因为这是手动创建活动是一个非常糟糕的主意。

final MainActivity c = new MainActivity(); 

并修复onProgressUpdate方法

protected void onProgressUpdate(String... title) {
    c.Header.setText("Text: " + title[0]);
}

答案 3 :(得分:0)

对于所有java.lang.Object []项,您逐个转换为字符串,然后逐个放入java.lang.String []。直接它将无法正常工作。