Java Android - getTabHost() 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2775859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - getTabHost() is undefined
提问by gbhall
I started learning Java and how to program for Android last night :)
我昨晚开始学习 Java 以及如何为 Android 编程 :)
So far I'm on this tutorial: developer.android.com/resources/tutorials/views/hello-tabwidget.html
到目前为止,我正在学习本教程:developer.android.com/resources/tutorials/views/hello-tabwidget.html
Clearly these tutorials have been designed for people that already have experience with Java.
显然,这些教程是为已经有 Java 经验的人设计的。
Despite the tutorial lacking all the required steps (for an absolute beginner with Java) and even having a typo, it seems I've figured everything out (which I'm really proud of :p). Except I cannot figure out how to fix TabHost tabHost = getTabHost();
which apparently is undefined.
尽管本教程缺少所有必需的步骤(对于 Java 的绝对初学者),甚至还有一个错字,但似乎我已经弄清楚了一切(我真的为此感到自豪 :p)。除了我无法弄清楚如何解决TabHost tabHost = getTabHost();
显然未定义的问题。
I have one other error shown in the linked image below, but I don't see why.
我在下面的链接图像中显示了另一个错误,但我不明白为什么。
Here is the linked image.
这是链接的图像。
Thanks, Gareth
谢谢,加雷斯
采纳答案by David
You just missed step 5 of the tutorial.
您刚刚错过了本教程的第 5 步。
The other error is due to the capital T on TabHost. You want: tabHost.addTab(spec);
另一个错误是由于 TabHost 上的大写字母 T。你要:tabHost.addTab(spec);
Java is case sensitive, so it makes a difference (calling a non-exsistant static method of the TabHost class vs. calling an instance method on the object referenced by the tabHost variable).
Java 区分大小写,因此它有所不同(调用 TabHost 类的不存在的静态方法与调用 tabHost 变量引用的对象上的实例方法)。
回答by AaronM
getTabHost() is a method of TabActivity not Activity. You'll need to extend that instead of Activity for the method to be defined in your object.
getTabHost() 是 TabActivity 而非 Activity 的方法。对于要在对象中定义的方法,您需要扩展它而不是 Activity。
回答by Boris Georgiev
Now that TabActivityis deprecated in API level 13, it is recommended to use Fragmentsinstead. Still possible, however, to use a support library for compatibility back to DONUT.
现在API 级别 13 中不推荐使用TabActivity,建议改用Fragments。然而,仍然可以使用支持库来兼容回 DONUT。