未定义的getTabHost()方法

时间:2012-05-28 15:40:50

标签: java android

我在android中制作标签。我正在使用TabHost来实现这一目标。我在此行getTabHost()上的TabHost tabHost = getTabHost();方法下获得了红线。我不知道为什么我会这样做。我怎样才能使这个例子有效?

这是我的完整代码

public class TabActivity extends Activity {
//TabHost mTabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    //Artist Tab
    intent = new Intent(this, Artist.class);
    spec = tabHost.newTabSpec("artists").setIndicator("Artist", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
    tabHost.addTab(spec);

  //Songs
    intent = new Intent(this, Songs.class);
    spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
    tabHost.addTab(spec);

  //Albums
    intent = new Intent(this, Album.class);
    spec = tabHost.newTabSpec("artists").setIndicator("Artist", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(1);
}
}

1 个答案:

答案 0 :(得分:2)

您需要扩展My TabActivity而不是简单的活动 尝试

public class MyTabActivity extends TabActivity 

而不是

public class MyTabActivity extends Activity