我有一个在 Internet 上请求数据信息的应用程序(客户端-服务器应用程序),但是这种通信非常慢,因此我决定创建一个 AsyncTask 来管理延迟。在 doInBackground 内部,我称之为Looper.prepare()
“视图生成器(检索数据)”。
详细(问题):
我有一个动态创建列表视图行的活动。但每次我尝试增加行时,android 都会抛出 Looper 异常“每个线程只能创建一个 Looper”
我按照以下步骤操作:
- 称呼
Looper.preapare()
- 使用第一次膨胀来创建我的列表的容器
- 使用第二次膨胀来创建列表行
我想我不能充气两次,但我不知道如何解决这个问题
异步任务
private class DrawerView extends AsyncTask<ActivityGroup, String, View>{
Exception exc=null;
@Override protected void onPreExecute() {
super.onPreExecute();
}
@Override protected View doInBackground(ActivityGroup... params) {
try {
Looper.prepare();
return processAct();
}catch (ConnectionException e) {
exc =e;
return null;
}
catch (Exception e) {
exc = e;
return null;
}
}
@Override protected void onPostExecute(View result) {
super.onPostExecute(result);
if(exc!= null){
Utils.usrMessage(getApplicationContext(), "Oh Noo!:\n"+exc.getMessage());
Utils.logErr(getApplicationContext(), exc);
finish();
}
if(result!= null){
setContentView(result);
}
}
}
processAct()
是这样实现的抽象方法
@Override protected View processAct() throws Exception {
Bundle bundle = getIntent().getExtras();
User user = (User)bundle.getSerializable("user");
Team team = Team.getTeamInformation(this,user.getTeamId());
ArrayList<Player> players =Player.getPlayerList(this,user.getTeamId());
PlayersListAdapter view = new PlayersListAdapter(this,players,team);
return view;
}
PlayerListAdapter
是构建/设置第一个视图(列表容器)的类..这里是第一个通货膨胀
public PlayersListAdapter(Context context, ArrayList<Player> players,Team team) throws Exception{
super(context);
View view = inflate(getContext(), R.layout.team_players, this);
TextView tv_teamName = (TextView)view.findViewById(R.id.tbplrs_tmnm);
TextView tv_playersNum = (TextView)view.findViewById(R.id.tbplrs_nplrs);
tv_teamName.setText(team.getName());
String msg = players.size()+" ";
msg += (players.size()!=1)?context.getString(R.string.playerPlural):context.getString(R.string.playerSingle);
tv_playersNum.setText(msg);
ListView lView = (ListView)view.findViewById(R.id.tbplrs_plrslst);
PlayersRowListAdapter plAdapter = new PlayersRowListAdapter(context, players);
lView.setAdapter(plAdapter);
}
最后PlayerRowListAdapter
扩展了 BaseAdapter,...这里是第二次通货膨胀
@Override public View getView(int position, View view, ViewGroup parent) {
if (view == null){
LayoutInflater lInflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = lInflator.inflate(R.layout.team_player_singlplayer,null);
}
....
....
}
注意如果我放弃第二个适配器PlayerRowListAdapter
......一切正常......(显然没有列表)
问候
ps对不起我的英语