我有以下代码来解析来自 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)
代码有什么问题?请帮我。谢谢。