0

我开发的应用程序将数据加载到 ArrayList 需要 2/3 秒。我想显示将数据加载到 ArrayList 的进度。

我正在为此使用 AsyncTask 。我在单击按钮时执行此操作,当 ArrayList 完全加载时,它将通过 Intent 传递给下一个活动。

代码片段如下 -

onClick 按钮 -

showList.setOnClickListener(new View.OnClickListener() {
  @Override
    public void onClick(final View v) {

      if (session.isOpened()) {

         if (friendID.size() == 0 && friendName.size() == 0) {

        new Async().execute();
      }
    }
});

Ansyc Task 类如下——

class Async extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        dialog.setTitle("Loading...");
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setMessage("Please wait for completion");
        dialog.setProgress(0);

        dialog.show();
    }

    @Override
    protected String doInBackground(String... params) {

        Request.executeMyFriendsRequestAsync(session,
            new Request.GraphUserListCallback() {

                            @Override
            public void onCompleted(List<GraphUser> users,
                            Response response) {

                 Iterator<GraphUser> iterator = users.iterator();
                 GraphUser graphUser = null;

                    friendID.clear();
                    friendName.clear();

                    while (iterator.hasNext()) {
                            if (iterator != null) {
                                graphUser = iterator.next();

                                Log.d("Friend Information ",
                                        " friend ID " + graphUser.getId()
                                                + " friend Name "
                                                + graphUser.getName());

                                friendID.add(graphUser.getId());
                                friendName.add(graphUser.getName());

                                publishProgress(friendID.size());

                                Log.d("Friend Information ", " friendID= "
                                        + friendID.size() + " friendName "
                                        + friendName.size());
                            }
                        }
                    }

                });

        dialog.dismiss();
        return "sucess";
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        Intent intent = new Intent(MainActivity.this, FriendsList.class);

        Log.d("Checking the size ", "ID " + friendID.size() + " UserName "
                + friendName.size());

        /*
         * Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT)
         * .show();
         */

        intent.putStringArrayListExtra("userID", friendID);
        intent.putStringArrayListExtra("userName", friendName);
        intent.putExtra("str", result);
        startActivity(intent);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        dialog.setProgress(values[0]);
        //dialog.incrementProgressBy(values[0]);
        super.onProgressUpdate(values);
    }
}

Log Cat 中的错误 -

04-12 14:15:56.595: E/AndroidRuntime(15794): FATAL EXCEPTION: AsyncTask #2
04-12 14:15:56.595: E/AndroidRuntime(15794): **java.lang.RuntimeException: An error occured while executing doInBackground()**

04-12 14:15:56.595: E/AndroidRuntime(15794):    at android.os.AsyncTask$3.done(AsyncTask.java:200)

04-12 14:15:56.595: E/AndroidRuntime(15794):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)

04-12 14:15:56.595: E/AndroidRuntime(15794):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)

04-12 14:15:56.595: E/AndroidRuntime(15794):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)

04-12 14:15:56.595: E/AndroidRuntime(15794):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)

04-12 14:15:56.595: E/AndroidRuntime(15794):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)

04-12 14:15:56.595: E/AndroidRuntime(15794):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)

04-12 14:15:56.595: E/AndroidRuntime(15794):    at java.lang.Thread.run(Thread.java:1019)
04-12 14:15:56.595: E/AndroidRuntime(15794): **Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()**
4

1 回答 1

1

2点:

  1. 看起来您Request.executeMyFriendsRequestAsync的操作是异步操作,因此您只需请求一些操作并快速从AsyncTask.doInBackground(). 因此,通过这样的设计,数据加载本身会在之后发生,当AsyncTask.doInBackground()已经通过时。您需要打开一个 sycn API,以便在AsyncTask.doInBackground().

  2. 调用dialog.dismiss();里面AsyncTask.onPostExecute()

于 2013-04-12T13:16:01.603 回答