Android TabHost 示例的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2209406/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:58:42  来源:igfitidea点击:

Issues with Android TabHost Example

androidandroid-tabhost

提问by KevinDTimm

I have been attempting to implement the 'advanced' tabwidgetexample from google. But, when it tries to call tabHost.addTab(spec);I get a stack trace from the debugger. Sorry, I don't have the stack trace here, but I'm wondering if others have had this same issue (as this code had a number of typo's and missing information that stopped me from even compiling.

我一直在尝试实现来自谷歌的“高级” tabwidget示例。但是,当它尝试调用时,tabHost.addTab(spec);我会从调试器获得堆栈跟踪。抱歉,我这里没有堆栈跟踪,但我想知道其他人是否也遇到过同样的问题(因为此代码有许多拼写错误和丢失的信息,这使我无法编译。

Can anyone point me to a corrected/running version of this code?

任何人都可以指出我此代码的更正/运行版本吗?

The updated information needed are:

需要更新的信息是:

<activity android:name=".ArtistsActivity"></activity>
<activity android:name=".AlbumsActivity"></activity>
<activity android:name=".SongsActivity"></activity>

回答by Ted

I spent the last hour or so going through that tutorial. Here's the problems and fixes for it that I dealt with:

我花了最后一个小时左右来完成那个教程。这是我处理的问题和修复方法:

Step 2:When creating your activities, if you do not create them through the manifest then you'll need to add them to the manifest manually.

第 2 步:创建活动时,如果您不通过清单创建它们,则需要手动将它们添加到清单中。

Add these lines to AndroidManifest.xml:

将这些行添加到 AndroidManifest.xml:

  <activity android:name=".AlbumsActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">
        </activity>
  <activity android:name=".ArtistsActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">
        </activity>
          <activity android:name=".SongsActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">
        </activity>

Step 3:You are only instructed to create the ic_tab_artists.xml file. You'll need to create one for ic_tab_songs.xml and ic_tab_albums.xml as well. You can just duplicate the ic_tab_artists.xml (or change the HelloTabView.java tab specs to use the artists.xml file for each tab).

第 3 步:仅指示您创建 ic_tab_artists.xml 文件。您还需要为 ic_tab_songs.xml 和 ic_tab_albums.xml 创建一个。您可以只复制 ic_tab_artists.xml(或更改 HelloTabView.java 选项卡规范以对每个选项卡使用 Artists.xml 文件)。

Step 4:The third to last line under /res/layout/main has a typo (a ; instead of a :)

第 4 步:/res/layout/main 下的倒数第三行有一个错字(a ; 而不是 a :)

      android:padding="5dp" />
    </LinearLayout>
</TabHost>

Step 6:There is a typo that uses calls mTabHost instead of tabHost. Change it.

第 6 步:有一个使用调用 mTab​​Host 而不是 tabHost 的错字。更改。

As already cited the getIntent() function on the last line isn't appropriate. I just call the tab based on it's id. eg:

正如已经提到的那样,最后一行的 getIntent() 函数是不合适的。我只是根据它的 id 调用选项卡。例如:

tabHost.setCurrentTabByTag("albums");

回答by crv

The current TabHost Exampleon the Android Developers site contains one error and also leaves out an important step that will prevent the example from running.

Android 开发人员站点上的当前TabHost 示例包含一个错误,并且还遗漏了一个会阻止示例运行的重要步骤。

First off: In the onCreate() method that is added to HelloTabWidget class attempts to use a TabHost object called mTabHost. This is invalid, it should be tabHost.

首先:在添加到 HelloTabWidget 类的 onCreate() 方法中,尝试使用名为 mTab​​Host 的 TabHost 对象。这是无效的,应该是tabHost。

Second: The tutorial leaves out the details that you need to add each of the activities too the AndroidManifest.xml. Without this the code will not work and you will get "force close" each time you attempt to execute.

第二:本教程省略了将每个活动添加到 AndroidManifest.xml 所需的详细信息。没有这个,代码将无法工作,每次尝试执行时都会“强制关闭”。

回答by Marco Hertwig

Also the

还有

android:theme="@android:style/Theme.NoTitleBar"

android:theme="@android:style/Theme.NoTitleBar"

does not work for me, if I replace it with

对我不起作用,如果我将其替换为

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

it works fine.

它工作正常。

回答by Lou

The example, as corrected by Ted, also works if all three activities do not include the line:

如果所有三个活动不包括以下行,则 Ted 更正的示例也适用:

android:theme="@android:style/Theme.NoTitleBar"

This is useful if you want a title bar in addition to the tab labels.

如果除了选项卡标签之外还需要标题栏,这很有用。