Why do I get an error while trying to set the content of a tabspec in android?

前端 未结 3 943
暗喜
暗喜 2020-12-18 07:37

I have an android activity in which I\'m using tabs.

public class UnitActivity extends TabActivity {
  @Override
  public void onCreate(Bundle savedInstanceS         


        
3条回答
  •  半阙折子戏
    2020-12-18 08:02

    While Activity.setContentView takes an id of a Layout, TabSpec.setContent takes an id of a View. This means you need to pass an id that looks like R.id.something and not R.layout.something.

    To solve your particular problem, give the top level view in your layout a view id:

    
      
      ...
    
    

    and update your source:

    public class UnitActivity extends TabActivity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.unit_view);
    
        TabHost tabHost = getTabHost();
        TabSpec spec;
    
        spec = tabHost.newTabSpec("controls");
        spec.setIndicator("Control");
        spec.setContent(R.id.unit_control);  // NOTE THE CHANGE
        tabHost.addTab(spec);
    
        spec = tabHost.newTabSpec("data");
        spec.setIndicator("Data");
        spec.setContent(R.id.unit_data);   // NOTE THE CHANGE
        tabHost.addTab(spec);
      }
    }
    

    For more information, see the tab examples in the ApiDemos:

提交回复
热议问题