2012-03-30 167 views
0

我想設置我的AsyncTask返回一個自定義的類類型,如爲什麼AsyncTask不接受我的類作爲返回參數?

AsyncTask<Void, Void, MyClass> 

爲此,我創建了一個類用戶。 文件: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' 

任何想法,爲什麼?

非常感謝您的幫助! 快樂編碼!

+6

你有錯誤信息什麼的? – Jeroen 2012-03-30 21:16:58

+1

您的代碼是否編譯或確實沒有設置字符串名稱?當你說「This is not working properly」時,請更具體一些。我注意到addName在正文中有一個錯字。 – Bill 2012-03-30 21:19:08

+3

你應該真的遵循大寫字母開頭類名的Java命名約定。其他任何事情都只是令人困惑。 – 2012-03-30 21:32:22

回答

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(); 
    } 
} 
+0

嘿,我認爲神祕的問題已經解決了。謝謝弗蘭克! – Eric 2012-04-02 16:54:40

相關問題