应用程序每次运行此代码时都会崩溃

时间:2014-01-28 14:14:46

标签: java android android-tabhost

每次运行此代码时,应用程序崩溃,有人知道为什么会发生这种情况。我真的很感激。提前致谢!代码很简单,我试图添加一个新选项卡,每次单击某个按钮并删除选择当前选项卡时的选项卡。

public class MainActivity extends Activity implements OnTabChangeListener {
    TabHost tabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setContentView(R.layout.activity_main);

        tabs=(TabHost)findViewById(R.id.tabhost);
    tabs.setup();


    TabHost.TabSpec spec= tabs.newTabSpec("buttontab");
    spec.setContent(R.id.buttontab);
    spec.setIndicator("Button");
    tabs.setCurrentTab(0);
    tabs.setOnTabChangedListener(this);

    Button btn=(Button)tabs.getCurrentView().findViewById(R.id.buttontab);
            btn.setOnClickListener(new View.OnClickListener() { 
public void onClick(View view) {
    TabHost.TabSpec spec=tabs.newTabSpec("tag1");

    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return(new AnalogClock(MainActivity.this));
        }
    });
    spec.setIndicator("Clock");
    tabs.addTab(spec);


    //tabs.getTabWidget().removeView(tabs.getTabWidget().getChildTabViewAt(3));

    }
        });


  }
@Override
public void onTabChanged(String arg0) {
    // TODO Auto-generated method stub
    int currentTabNumber = tabs.getCurrentTab();

Toast.makeText(getBaseContext(), "current tab:" +        
currentTabNumber,Toast.LENGTH_SHORT).show();                  
//Toast.makeText(getBaseContext(), Counts, Toast.LENGTH_SHORT).show();
tabs.getTabWidget().removeView(tabs.getTabWidget().getChildTabViewAt(currentTabNumbe    r));

}
}

0 个答案:

没有答案