2

I need to put a browser like module in activity. I am using fragmentTabHost for this. Add button will Add the Tab in fragmentTabHost and and there is Close button to Close the tab from fragmentTabHost. .But It work fine for 3 to 4 time but after that it throws the NullPointerException

I think this exception occurs after closing a tab i.e. here is no error when add a new tab. But after closing a tab if i try to select last or second last tab then exception occurs.

here is code of my activity

public class MutiTabActivity extends FragmentActivity {
 TabHost tabHost;
 private FragmentTabHost fragmentTabHost;
 private Button addButton;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  addButton = (Button) findViewById(R.id.add_new_tab);
  addButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    addNewTab();
   }
  });

  fragmentTabHost = (FragmentTabHost) findViewById(R.id.main_tab_host);
  fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.frame_layout);

  fragmentTabHost.getTabWidget().setDividerDrawable(android.R.color.white);

  fragmentTabHost.setOnTabChangedListener(new OnTabChangeListener() {

   @Override
   public void onTabChanged(String tabId) {
    onTabSelected();
   }
  });

  addNewTab();
  addNewTab();
  addNewTab();
  addNewTab();
 }

 OnClickListener closeTabListener = new OnClickListener() {

  @Override
  public void onClick(View v) {
   TabWidget tabWidget = fragmentTabHost.getTabWidget();
   tabWidget.removeViewAt(currentTabIndex);
  }
 };
 private int currentTabIndex;

 /*
  * used to change the background color of selected tab and show close button
  * on selected tab
  */
 private void onTabSelected() {
  TabWidget tabWidget = fragmentTabHost.getTabWidget();
  int tabCount = tabWidget.getTabCount();
  for (int i = 0; i < tabCount; i++) {
   View childAt = tabWidget.getChildAt(i);
   View findViewById = childAt.findViewById(R.id.close_tab_textView1);
   if (findViewById != null) {
    findViewById.setVisibility(View.GONE);
    childAt.setBackgroundColor(Color.LTGRAY);
    findViewById.setOnClickListener(null);

    String currentTabTag = fragmentTabHost.getCurrentTabTag();

    String tag2 = (String) childAt.getTag();

    if (tag2 != null) {
     if (currentTabTag.equals(tag2)) {
      findViewById.setVisibility(View.VISIBLE);
      findViewById.setOnClickListener(closeTabListener);
      childAt.setBackgroundColor(Color.DKGRAY);
      currentTabIndex = i;
     }
    }
   }
  }
 }

 private int lastTabCount;

 private TabSpec getNewTab(String tag, String title) {

  TabSpec newTabSpec = fragmentTabHost.newTabSpec(tag);
  TabWidget tabWidget = fragmentTabHost.getTabWidget();
  View view2 = LayoutInflater.from(this).inflate(R.layout.tab_view, tabWidget, false);
  view2.setTag(tag);
  TextView name = (TextView) view2.findViewById(R.id.textView1);
  TextView closeBtn = (TextView) view2.findViewById(R.id.close_tab_textView1);
  closeBtn.setOnTouchListener(new OnTouchListener() {

   @Override
   public boolean onTouch(View v, MotionEvent event) {
    return false;
   }
  });
  name.setText(title);
  newTabSpec.setIndicator(view2);
  return newTabSpec;
 }

 /* add new tab in fragmentTabHost */
 private void addNewTab() {
  lastTabCount++;
  fragmentTabHost.addTab(getNewTab("Tag " + lastTabCount, "Tab " + lastTabCount), WebViewTab.class, null);
 }
}

Following is log

    05-11 03:48:27.652: E/AndroidRuntime(11073): FATAL EXCEPTION: main
05-11 03:48:27.652: E/AndroidRuntime(11073): java.lang.NullPointerException
05-11 03:48:27.652: E/AndroidRuntime(11073):    at android.widget.TabWidget.focusCurrentTab(TabWidget.java:461)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at android.widget.TabHost.setCurrentTab(TabHost.java:410)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at android.view.View.performClick(View.java:4240)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at android.view.View$PerformClick.run(View.java:17721)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at android.os.Handler.handleCallback(Handler.java:730)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at android.os.Looper.loop(Looper.java:137)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at android.app.ActivityThread.main(ActivityThread.java:5103)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at java.lang.reflect.Method.invokeNative(Native Method)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at java.lang.reflect.Method.invoke(Method.java:525)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-11 03:48:27.652: E/AndroidRuntime(11073):    at dalvik.system.NativeStart.main(Native Method)

I found problem like this here but solution provided there doesn't work for me.

Thanks in advance, please help me out.

Community
  • 1
  • 1

0 Answers0