Android:getTabHost()错误

时间:2012-02-08 18:53:08

标签: android android-layout

我是Android和Java开发的新手,我正在关注Android Dev网站上的Tab Layout教程。我遇到了代码问题。只需使用一些复制面食魔法来确保我没有拼写错误,我已经插入了教程所需的所有代码。问题是getTabHost()是未定义的......以下是代码:

public class AHActivity extends Activity {
/** 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

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, EvoActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("evolution").setIndicator("Evolution")
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, physActivity.class);
    spec = tabHost.newTabSpec("physics").setIndicator("Physics")
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(2);
    }
    }

1 个答案:

答案 0 :(得分:5)

您的活动应扩展TabActivity而非活动。

public class AHActivity extends TabActivity {

注意:最新版本中不推荐使用Tabactivitity,请考虑学习Fragments