error android.view.ViewRoot$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.
代码是
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String arg0) {
if (arg0.equals("tab1")) {
Log.v("Hi","Tilsan");
BS_Main.submit.setVisibility(View.INVISIBLE);
alt_header_text.setVisibility(View.INVISIBLE);
header_text.setText("Articles");
tabHost.setCurrentTab(1);
if (temp_stat.equals("lv")) {
Log.v("Hi+++","Tilsan");
alt_header_text.setVisibility(View.INVISIBLE);
back.setVisibility(View.INVISIBLE);
//share.setVisibility(View.INVISIBLE);
gal_lay.setVisibility(View.VISIBLE);
//lv_movies.setVisibility(View.GONE);
lv_movies.setVisibility(View.VISIBLE);
url = "http://www.theblacksheeponline.com/mobile/article.php?start=1&showcount=-1&tag=all";
flyurl = url;
san_flag=true;
new San_FirstTask().execute();
}
} else if (arg0.equals("tab2")) {
BS_Main.submit.setVisibility(View.INVISIBLE);
// visiblity();
header_text.setText("Schools");
alt_header_text.setVisibility(View.INVISIBLE);
if (BS_Bars.temp_bars == 0) {
BS_Main.submit.setVisibility(View.INVISIBLE);
BS_Bars.lv_bardate.setVisibility(View.GONE);
BS_Bars.lv_barlist.setVisibility(View.GONE);
// BS_Bars.lv_barlist_details.setVisibility(View.GONE);
BS_Bars.lv.setVisibility(View.VISIBLE);
back.setVisibility(View.INVISIBLE);
gal_lay.setVisibility(View.GONE);
} else if (BS_Bars.temp_bars == 1) {
BS_Main.submit.setVisibility(View.INVISIBLE);
BS_Bars.lv.setVisibility(View.GONE);
BS_Bars.lv_barlist.setVisibility(View.GONE);
// BS_Bars.lv_barlist_details.setVisibility(View.GONE);
BS_Bars.lv_bardate.setVisibility(View.VISIBLE);
back.setVisibility(View.VISIBLE);
gal_lay.setVisibility(View.GONE);
} else if (BS_Bars.temp_bars == 2) {
BS_Main.submit.setVisibility(View.INVISIBLE);
BS_Bars.lv_bardate.setVisibility(View.GONE);
BS_Bars.lv.setVisibility(View.GONE);
BS_Bars.lv_barlist.setVisibility(View.VISIBLE);
back.setVisibility(View.VISIBLE);
gal_lay.setVisibility(View.GONE);
} else if (BS_Bars.temp_bars == 3) {
BS_Main.submit.setVisibility(View.INVISIBLE);
BS_Bars.lv_bardate.setVisibility(View.GONE);
BS_Bars.lv.setVisibility(View.GONE);
BS_Bars.lv_barlist.setVisibility(View.GONE);
back.setVisibility(View.VISIBLE);
gal_lay.setVisibility(View.GONE);
} else if (BS_Bars.temp_bars == 4) {
BS_Main.submit.setVisibility(View.INVISIBLE);
BS_Bars.lv_bardate.setVisibility(View.GONE);
BS_Bars.lv.setVisibility(View.GONE);
BS_Bars.lv_barlist.setVisibility(View.GONE);
BS_Bars.bars_wv.setVisibility(View.VISIBLE);
back.setVisibility(View.VISIBLE);
gal_lay.setVisibility(View.GONE);
} else {
BS_Main.submit.setVisibility(View.INVISIBLE);
BS_Bars.lv.setVisibility(View.VISIBLE);
BS_Bars.lv_barlist.setVisibility(View.GONE);
BS_Bars.lv_bardate.setVisibility(View.GONE);
back.setVisibility(View.GONE);
gal_lay.setVisibility(View.GONE);
}
} else if (arg0.equals("tab3")) {
back.setVisibility(View.GONE);
gal_lay.setVisibility(View.GONE);
alt_header_text.setVisibility(View.INVISIBLE);
header_text.setText("ShoutOuts");
BS_Main.submit.setVisibility(View.INVISIBLE);
tabHost.setCurrentTab(3);
BS_Shoutout.shout_realtive.setVisibility(View.VISIBLE);
} else if (arg0.equals("tab4")) {
Log.v("Tag", "TAB4");
back.setVisibility(View.GONE);
//share.setVisibility(View.GONE);
gal_lay.setVisibility(View.GONE);
temp_stat1 = "imgv";
header_text.setText("Pictures");
alt_header_text.setVisibility(View.INVISIBLE);
Log.v("TAG", "Pictures");
tabHost.setCurrentTab(4);
} else if (arg0.equals("tab5")) {
Intent obj_intent = new Intent(BS_Main.this,
Rss_Feed_Grid.class);
startActivity(obj_intent);
}
}
});
// Asynchronous Task class code
private class San_FirstTask extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(BS_Main.this);
String san_url;
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
if(san_flag == true)
{
Log.v("Hai Sankar","san_flag==true");
san_url="http://www.theblacksheeponline.com/mobile/article.php?start=1&showcount=-1&tag="+san_id[0];
Log.v("SanUrl_here", san_url);
slist = new MultiLine_Sex(BS_Main.this, san_url);
lv_movies.addFooterView(footerView, null, true);
footerView.setVisibility(View.VISIBLE);
san_flag=false;
}
else
{
Log.v("Url_here", url);
slist = new MultiLine_Sex(BS_Main.this, url);
}
Log.v(TAG, "progress");
} catch (Exception e) {
Log.v(TAG, "error " + e);
}
return null;
}
protected void onPostExecute(Void result) {
Log.v("Hai","Inside Else in Atask");
// footerView.setVisibility(View.VISIBLE);
// ListView.FixedViewInfo footerInfo1 = lv_movies.new FixedViewInfo();
// footerInfo1.isSelectable=true;
// ArrayList<ListView.FixedViewInfo> footerInfos1=new ArrayList<ListView.FixedViewInfo>();
// footerInfo1.view = footerView;
// footerInfos1.add(footerInfo1);
// HeaderViewListAdapter headerViewListAdapter1 = new HeaderViewListAdapter(null,footerInfos1,slist);
// lv_movies.setAdapter(headerViewListAdapter1);
//lv_movies.addFooterView(footerView);
//footerView.setVisibility(View.VISIBLE);
lv_movies.setAdapter(slist);
slist.notifyDataSetChanged();
// code blocked by sandher.......
// if (san_url != null) {
// Log.v("Hai","Inside If in Atask");
// footerView.setVisibility(View.VISIBLE);
// ListView.FixedViewInfo footerInfo = lv_movies.new FixedViewInfo();
// footerInfo.isSelectable=false;
// ArrayList<ListView.FixedViewInfo> footerInfos=new ArrayList<ListView.FixedViewInfo>();
// footerInfo.view = footerView;
// footerInfos.add(footerInfo);
// HeaderViewListAdapter headerViewListAdapter = new HeaderViewListAdapter(null,footerInfos,slist);
// lv_movies.setAdapter(headerViewListAdapter);
// //lv_movies.addFooterView(footerView);
////
//// lv_movies.addFooterView(footerView);
//// lv_movies.setAdapter(slist);
// slist.notifyDataSetChanged();
// }
// else if (url != null) {
// Log.v("Hai","Inside Else in Atask");
// footerView.setVisibility(View.VISIBLE);
// ListView.FixedViewInfo footerInfo1 = lv_movies.new FixedViewInfo();
// footerInfo1.isSelectable=true;
// ArrayList<ListView.FixedViewInfo> footerInfos1=new ArrayList<ListView.FixedViewInfo>();
// footerInfo1.view = footerView;
// footerInfos1.add(footerInfo1);
// HeaderViewListAdapter headerViewListAdapter1 = new HeaderViewListAdapter(null,footerInfos1,slist);
// lv_movies.setAdapter(headerViewListAdapter1);
//
// //lv_movies.addFooterView(footerView);
// //lv_movies.setAdapter(slist);
// slist.notifyDataSetChanged();
// }
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
答案 0 :(得分:3)
单击第一个选项卡时,您正在启动异步线程(通过AsyncTask
)。在doInBackground()
(在异步线程中运行的部分)中,您调用两个修改UI的方法
lv_movies.addFooterView(footerView, null, true);
footerView.setVisibility(View.VISIBLE);
很明显其中一个导致错误。 所有 UI操作都应该在UI线程上运行。如果您无法重新排列代码以在onPreExecute()
或onPostExecute()
上执行这些操作,请将其打包到Activity.runOnUIThread()
或View.post()
。
在这种情况下,一个简单的解决方法是将上面两行更改为
footerView.post(new Runnable() {
run() {
lv_movies.addFooterView(footerView, null, true);
footerView.setVisibility(View.VISIBLE);
}
});
这将把Runnable放入“消息队列”中,一旦该线程处理完所有先前的事件,它就会使它在UI线程上运行。 (UI线程只是一个循环,它不断查找要添加到消息队列的新事件,并按接收的顺序执行它们。)