我在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);
}
}

最佳答案

您需要扩展“我的活动”而不是简单的活动
托盘

public class MyTabActivity extends TabActivity

代替
public class MyTabActivity extends Activity

10-05 18:24