3

我正在使用 Twitter4J 库,并且正在尝试实现一些异步任务来对服务进行一些调用,如下所示。

public class TweetsActivity extends Activity {


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

}
@SuppressWarnings("unused")
private class getFollowers extends AsyncTask<String,Void,List<String>>
{

    @Override
    protected List<String> doInBackground(String... arg0) {
        List<String> friends = null;
        Twitter t = ((TwitterApplication)getApplication()).getTwitter();
        try {
            IDs names  = t.getFollowersIDs("jd_biggestboss");
            for(int i:names.getIDs())
            {
                friends.add(t.showUser(i).getScreenName());

            }
            return friends;
        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }// TODO Auto-generated method stub
        return null;
    }

    protected void onPostExecute(List<String> result) {
        for(String name:result)
            System.out.println(name);

     }

}

}

我收到一个错误。这是堆栈跟踪。我检查以确保我没有从我的 UI 线程等访问任何 UI 元素。那么可能导致问题的原因是什么?难道我对 API 的每个调用都需要在一个单独的异步任务中吗?提前致谢。

01-27 02:22:58.042: W/dalvikvm(30528): threadid=11: thread exiting with uncaught exception (group=0x40ab0a08)
01-27 02:22:58.052: E/AndroidRuntime(30528): FATAL EXCEPTION: AsyncTask #1
01-27 02:22:58.052: E/AndroidRuntime(30528): java.lang.RuntimeException: An error occured while executing doInBackground()
01-27 02:22:58.052: E/AndroidRuntime(30528):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
01-27 02:22:58.052: E/AndroidRuntime(30528):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-27 02:22:58.052: E/AndroidRuntime(30528):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-27 02:22:58.052: E/AndroidRuntime(30528):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-27 02:22:58.052: E/AndroidRuntime(30528):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-27 02:22:58.052: E/AndroidRuntime(30528):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-27 02:22:58.052: E/AndroidRuntime(30528):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-27 02:22:58.052: E/AndroidRuntime(30528):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-27 02:22:58.052: E/AndroidRuntime(30528):    at java.lang.Thread.run(Thread.java:864)
01-27 02:22:58.052: E/AndroidRuntime(30528): Caused by: java.lang.NullPointerException
01-27 02:22:58.052: E/AndroidRuntime(30528):    at com.tmm.android.twitter.TweetsActivity$getFollowers.doInBackground(TweetsActivity.java:40)
01-27 02:22:58.052: E/AndroidRuntime(30528):    at com.tmm.android.twitter.TweetsActivity$getFollowers.doInBackground(TweetsActivity.java:1)
01-27 02:22:58.052: E/AndroidRuntime(30528):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-27 02:22:58.052: E/AndroidRuntime(30528):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-27 02:22:58.052: E/AndroidRuntime(30528):    ... 5 more
4

1 回答 1

1

ArrayList在 doInBackground 方法中添加元素之前,您需要初始化朋友:

List<String> friends = new ArrayList<String>();
于 2013-01-27T07:44:08.500 回答