Honeycomb 和 TabHost 规格 [英] Honeycomb and TabHost specs

查看:23
本文介绍了Honeycomb 和 TabHost 规格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Honeycomb 的向后兼容性有疑问.我有一个支持 2.1 或更高版本的应用,似乎主要在 Honeycomb 上运行,除非它们启动 TabActivity.

I have a question about Honeycomb's backward compatibility. I have an app that supports 2.1 or higher and seems to mostly work on Honeycomb except when they start a TabActivity.

特别是,当我向 TabHost 添加选项卡时,出现以下异常

In particular, when I add tabs to the TabHost, I get the following exception

android.content.res.Resources$NotFoundException:资源 ID #0x0

android.content.res.Resources$NotFoundException: Resource ID #0x0

查看引发此异常的代码时,我看到它是具有标签和图标的选项卡规范.在代码内部,在 LabelAndIconIndicatorStrategy 中尝试膨胀布局文件 R.layout.tab_indicator,该文件似乎不可用.

When looking at the code that throws this exception, I see that it's the tab spec that has a label and an icon. Inside the code, in the LabelAndIconIndicatorStrategy tries to inflate the layout file R.layout.tab_indicator which doesn't appear to be available.

    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(_gameActivity, ScoreGameActivity.class);
    intent.putExtra(GameChangerConstants.STREAM_ID, _stream.pk().toString());

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = _gameTabHost.newTabSpec("score_game").setIndicator("Score", res.getDrawable(R.drawable.icon_field_gloss)).setContent(intent);
    _gameTabHost.addTab(spec);

有没有一种我不知道的为蜂窝创建标签的新方法?我已经翻阅了文档,但没有看到任何表明我所做的事情有问题的地方.

Is there a new way of creating tabs for honeycomb that I don't know about? I've poured over the documentation but haven't seen anything that indicates a problem with what I've done.

在我们可以对 UI 小部件进行更全面的重构之前,我想避免在这一点上使用片段,并且我想更好地理解这个问题.

I'd like to avoid having to use fragments at this point until we can do a more comprehensive restructuring of our UI widgets and I'd like to better understand this issue.

推荐答案

我相信我已经找到了解决方案,但是因为人们很好奇,这里是我遇到这个问题时得到的堆栈跟踪:

I believe I've found a solution, but because people are curious, here is the stacktrace I got when I ran into this problem:

05-17 13:09:53.462: ERROR/CustomExceptionHandler(500): Uncaught throwable in thread Thread[main,5,main]
    android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:1014)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2039)
    at android.content.res.Resources.getLayout(Resources.java:853)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:389)
    at android.widget.TabHost$LabelAndIconIndicatorStrategy.createIndicatorView(TabHost.java:568)
    at android.widget.TabHost.addTab(TabHost.java:226)
    at com.myApp.ui.TabDialog.addTab(TabDialog.java:80)
    ...

在那一行,我的代码大致相当于 sparky 看到的代码:

At that line, I have code roughly equivalent to what sparky saw:

spec = myTabHost.newTabSpec("score_game").setIndicator("Score", res.getDrawable(R.drawable.icon_field_gloss)).setContent(intent);
myTabHost.addTab(spec);

请注意,myTabHost 是 TabHost,spec 是 TabSpec.

Note that myTabHost is a TabHost and spec is a TabSpec.

以前,我是这样初始化 myTabHost 的:

Previously, I was initializing myTabHost like this:

//WRONG - This can CRASH your app starting at Android SDK 3.0
TabHost myTabHost = new TabHost(getContext()); 

为了解决这个问题,我通过这样做开始初始化 TabHost:

To fix this problem, I started initializing the TabHost by doing this:

TabHost myTabHost = new TabHost(getContext(), null); 

然后就解决了!我很想找到根本原因,但我还没有弄清楚.

And that fixed it! I would love to find a root cause, but I haven't yet been able to figure it out.

这篇关于Honeycomb 和 TabHost 规格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆