无法在没有调用Looper.prepare()的线程内创建处理程序是什么意思?

时间:2013-01-18 08:15:20

标签: android

我有以下代码来解析来自Facebook的JSon响应。:

doInBackground(JSONObject... params) {
   try {
     JSONArray posts = params[0].getJSONArray("notifications");
     notifications.clear();
     for (int i = 0; i < posts.length(); i++) {
     Notifications info = new Notifications();
     JSONObject tempobj = (JSONObject) posts.get(i);
     //some code here related to parse...
       }
     }catch(Ex...xxx){}
     sort();
}

我的排序方法如下:

private void sort() {
        mActivity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Log.e("sort called", "sort called" + actualListView.getSelectedItemPosition());
                try {
                    Collections.sort(notifications, new Comparator<Notifications>() {

                        @Override
                        public int compare(Notifications object1, Notifications object2) {

                            int time1 = Integer.parseInt(object1.updated_time);
                            int time2 = Integer.parseInt(object2.updated_time);
                            if (time1 < time2)
                                return 1;
                            else if (time1 > time2)
                                return -1;
                            return 0;
                        }
                    });
                } catch (Exception e) {
                    Log.e("java.util.Collections", "Error occurred in sorting the notifications");
                    e.printStackTrace();
                }
            }
        });

    }

在onPostExecute方法中我调用另一个方法来更新UI: 但我面临例外。 日志在这里:

STACK_TRACE=java.lang.ExceptionInInitializerError
at
com.platinumapps.fragments.NotificationFragment$NotificationRequestListener.onComplete(NotificationFragment.java:241)
at
com.facebook.android.AsyncFacebookRunner$3.run(AsyncFacebookRunner.java:391)
Caused by: java.lang.RuntimeException: Can't create handler inside thread
that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
... 2 more
java.lang.RuntimeException: Can't create handler inside thread that has not
called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
at
com.platinumapps.fragments.NotificationFragment$NotificationRequestListener.onComplete(NotificationFragment.java:241)
at
com.facebook.android.AsyncFacebookRunner$3.run(AsyncFacebookRunner.java:391)

代码有什么问题?请帮我。感谢。

1 个答案:

答案 0 :(得分:2)

您正在sort()中呼叫doInBackground,呼叫runOnUiThread ...将{pert}移至onPostExecute,因为您无法在runOnUiThread中呼叫doInBackground 1}} ...

 doInBackground(JSONObject... params) {
 try {
 JSONArray posts = params[0].getJSONArray("notifications");
 notifications.clear();
 for (int i = 0; i < posts.length(); i++) {
 Notifications info = new Notifications();
 JSONObject tempobj = (JSONObject) posts.get(i);
 //some code here related to parse...
   }
 }catch(Ex...xxx){}
 //sort();  //need to be moved
}

onPostExecute() {
   sort();
  .....
}