我从Facebook收到朋友列表并在列表视图中填充它工作正常但现在我正在尝试添加进度条直到列表视图填充但得到异常我已经尝试搜索但找不到我的问题的解决方案,这里是我的代码:
public class FriendsProgress extends AsyncTask<Object, Void, Boolean>
{
@Override
protected Boolean doInBackground(Object... Params)
{
try
{
getFriendList();
friendAdapter = new FriendAdapter(Friends.this, R.layout.activity_friends, friendsList);
Friends.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
list.setAdapter(friendAdapter);
//list.setOnItemClickListener(EditStaff.this);
}
});
}
catch (Exception e)
{
System.out.println("StaffProgess Exception Caught:"+e.getMessage());
}
return Boolean.TRUE;
}
@Override
protected void onPreExecute()
{
friendsProgress=new ProgressDialog(Friends.this);
friendsProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
friendsProgress.setIndeterminate(true);
friendsProgress.setMessage("Loading...");
friendsProgress.setOwnerActivity(Friends.this);
friendsProgress.show();
}
@Override
protected void onPostExecute(Boolean Result)
{
friendsProgress.dismiss();
friendAdapter.notifyDataSetChanged();
}
}
这里是getFriendsList函数实现:
public void getFriendList(){
Request request = Request.newMyFriendsRequest(Session.getActiveSession(), new Request.GraphUserListCallback() {
@Override
public void onCompleted(List<GraphUser> users, Response response) {
// TODO Auto-generated method stub
for(int i=0; i<users.size();i++)
{
GraphUser user = users.get(i);
Friend objFriend = new Friend();
objFriend.setFriendID(user.getId());
objFriend.setFriendName(user.getName());
Drawable dd =Friends.LoadImageFromWebOperations("http://graph.facebook.com/" + objFriend.getFriendID() + "/picture");
objFriend.setFriendPic(dd);
//objFriend.setFriendPic("http://graph.facebook.com/" + objFriend.getFriendID() + "/picture");
friendsList.add(objFriend);
//friendAdapter.notifyDataSetChanged();
Log.d("Friend's Id", objFriend.getFriendID());
Log.d("Friend's Name", objFriend.getFriendName());
//Log.d("Friend's Pic", objFriend.getFriendPic());
Log.d("Friend's List Count", Integer.toString(friendsList.size()));
}
}
});
Request.executeBatchAsync(request);
答案 0 :(得分:2)
您不应该实例化Adapter
并将其链接到ListView
方法中的doInBackground()
,因为它在工作线程上运行,并且没有连接到Android {{1允许在工作线程上执行。相反,您可能想要移动此代码
Views
到friendAdapter = new FriendAdapter(Friends.this, R.layout.activity_friends, friendsList);
Friends.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
list.setAdapter(friendAdapter);
//list.setOnItemClickListener(EditStaff.this);
}
});
方法。这肯定是一个更好的设计解决方案。希望这会有所帮助。
答案 1 :(得分:0)
试试这个,
ProgressDialog dialog;
protected void onPreExecute()
{
dialog = new ProgressDialog(Friends.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMax(100);
dialog.show();
}
答案 2 :(得分:0)
在onPostExecute()
方法中运行Ui Thread,如下所示:
runOnUiThread(new Runnable() {
@Override
public void run() {
friendAdapter = new FriendAdapter(Friends.this, R.layout.activity_friends, friendsList);
list.setAdapter(friendAdapter);
friendsProgress.dismiss();
friendAdapter.notifyDataSetChanged();
}
});