为什么AsyncTask不接受我的类作为返回参数?

时间:2012-03-30 21:14:06

标签: java android multithreading class android-asynctask

我正在尝试将AsyncTask设置为返回自定义类类型,例如

AsyncTask<Void, Void, MyClass>

为此,我创建了一个类User。 文件:User.java

package com.asynctask.namespace;
import java.util.ArrayList;

public class User
{   
    private ArrayList<String> name;

    public void addName(String value)
    {       
        name.add(value);
    }

    public String getName(int pos)
    {       
        return name.get(pos).toString();
    }
}

在我的主要活动中,我有:

package com.asynctask.namespace;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

public class AsyncTaskExampleActivity extends Activity {
    private TextView result;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);

        result = new TextView(this);
        setContentView(result);

        new loadData().execute();        
    }
    // --- Subclass --- //
    private class loadData extends AsyncTask<Void, Void, User> 
    {    
        ProgressDialog progDialog;
        User client = new User();

        protected void onPreExecute() 
        {
            progDialog = new ProgressDialog(AsyncTaskExampleActivity.this);
            progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progDialog.setMessage("Conectando a base de dados...");
            progDialog.show();
        }

        protected User doInBackground(Void... args) 
        {
            client.addName("testName");         
            return client;
        }

        protected void onPostExecute(User values) 
        {                   
            progDialog.dismiss();
            result.setText(values.getName(0));

        }

    }

}

我收到以下Logcat错误:

04-02 13:45:05.683: W/dalvikvm(1056): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
04-02 13:45:05.683: E/AndroidRuntime(1056): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
04-02 13:45:05.783: E/AndroidRuntime(1056): java.lang.RuntimeException: An error occured while executing doInBackground()
04-02 13:45:05.783: E/AndroidRuntime(1056):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.lang.Thread.run(Thread.java:1096)
04-02 13:45:05.783: E/AndroidRuntime(1056): Caused by: java.lang.NullPointerException
04-02 13:45:05.783: E/AndroidRuntime(1056):     at com.asynctask.namespace.User.addName(User.java:11)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at com.asynctask.namespace.AsyncTaskExampleActivity$loadData.doInBackground(AsyncTaskExampleActivity.java:39)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at com.asynctask.namespace.AsyncTaskExampleActivity$loadData.doInBackground(AsyncTaskExampleActivity.java:1)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-02 13:45:05.783: E/AndroidRuntime(1056):     ... 4 more
04-02 13:45:05.903: I/dalvikvm(1056): threadid=7: reacting to signal 3
04-02 13:45:06.093: I/dalvikvm(1056): Wrote stack trace to '/data/anr/traces.txt'

任何想法为什么?

非常感谢您的帮助! 快乐的编码!

1 个答案:

答案 0 :(得分:0)

您必须在使用之前初始化数组列表name。试试这个:

package com.asynctask.namespace;
import java.util.ArrayList;

public class User
{   
    // initialize before use
    private ArrayList<String> name = new ArrayList<String>();

    public void addName(String value)
    {       
        name.add(value);
    }

    public String getName(int pos)
    {       
        return name.get(pos).toString();
    }
}