最初我的应用程序显示5个相对布局。如果单击相对布局,则会加载线性布局;此时我想显示进度条。
我使用了以下代码作为进度条,但我得到了例外
>android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
我该如何处理?
代码
private OnClickListener exit1Listener = new OnClickListener()
{
public void onClick(View v)
{
if(!exit1status)
{
System.out.println("exit1 visible");
// Exit1 Restaurants
if(RB_Constant.upcomingexits_obj.response.size() > 0)
{ i if(RB_Constant.upcomingexits_obj.response.get(0).listRestaurants.size() > 0)
{
//YRS: listview1.setAdapter(new UpcomingResultsListViewAdapter1(this));
// Create the views on the fly instead of using the ListView
rbupcadapter1 = new UpcomingResultsListViewAdapter1(RB_UpcomingExits.this);
if(RB_Constant.upcomingexits_obj.response.get(0).listRestaurants.size() > 0)
{
numItems1 = RB_Constant.upcomingexits_obj.response.get(0).listRestaurants.size();
}
Exitexpand_pd = ProgressDialog.show(RB_UpcomingExits.this, "", "Please wait...", true);
Thread t = new Thread()
{
public void run()
{
try
{
for(position=0; position < numItems1; position++)
{
View convertview = null;
convertview = rbupcadapter1.getView(position, convertview, null); listLayout1.addView(convertview);
}
}
catch(Exception e)
{ System.out.println("Exit1Listener error ->"+e.toString());
} rb_Exitexpand_Handler.post(null);
Exitexpand_pd.dismiss();
}
};
t.start();
//listview1.setMinimumHeight(2000);
}
}
else{
//toastMsg("No results!");
}
listLayout1.setVisibility(View.VISIBLE);
exit1status = true;
if(exit2status || exit3status || exit4status || exit5status)
{
//System.out.println("exit2 GONE");
listLayout2.setVisibility(View.GONE);
listLayout3.setVisibility(View.GONE);
listLayout4.setVisibility(View.GONE);
listLayout5.setVisibility(View.GONE);
}
}
else
{
System.out.println("exit1 GONE");
listLayout1.setVisibility(View.GONE);
exit1status = false;
}
}
};
答案 0 :(得分:0)
你无法从UI线程以外的其他线程更新UI(视图)对象,尝试使用Handler对象。