tags:

views:

19

answers:

1

Hai Friends, I have implemented tab Listener, when i am clicking on the tabs it loads the data in the list view with no problem, if i click other tab options it also load fine, again if i click the first one i am getting the following the exception in my log, i do not know why i am getting this?, so please help me on this.

     error android.view.ViewRoot$CalledFromWrongThreadException: 
Only the original thread that created a view hierarchy can touch its views.

The code is

    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&amp;showcount=-1&amp;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();
   }
  }
 }
+1  A: 

You are starting an asynchronous thread (via AsyncTask) when the first tab is clicked. Within doInBackground() (which is the portion that runs in the asynchronous thread), you call two methods which modify the UI

            lv_movies.addFooterView(footerView, null, true);
            footerView.setVisibility(View.VISIBLE);

Pretty clearly one of these is causing the error. All UI operations should be run on the UI thread. If you can't rearrange your code to execute these operations on onPreExecute() or onPostExecute(), then wrap them in a call to Activity.runOnUIThread() or View.post().


A simple fix in this case would be to change the above two lines to

  footerView.post(new Runnable() {
    run() {
      lv_movies.addFooterView(footerView, null, true);
      footerView.setVisibility(View.VISIBLE);
    }
  });

This will put the Runnable into the "message queue" which will cause it to be run on the UI thread as soon as that thread is finished processing all previous events. (The UI thread is just a loop which continuously looks for new events to be added to the message queue and executes them in the order received.)

beekeeper
@Mr.Beekeeper: Thanks for ut help, friend,can u pls explain how to use Activity.runOnUIThread() or View.post(), so show me link which encomposes an example for this.
Tilsan The Fighter
Answer updated with an example.
beekeeper
@Mr.Beekeeper: Thanks for ur replay,u r correct friend., u fixed that error, thanks friend.Thanks U Very Much!!!!!!!!!!!!!!!!!!!!!
Tilsan The Fighter