我对 Honeycomb 的向后兼容性有疑问。我有一个支持 2.1 或更高版本的应用程序,似乎主要在 Honeycomb 上运行,除非它们启动 TabActivity。
特别是,当我将选项卡添加到 TabHost 时,出现以下异常
android.content.res.Resources$NotFoundException: 资源 ID #0x0
在查看引发此异常的代码时,我发现它是具有标签和图标的选项卡规范。在代码中,在 LabelAndIconIndicatorStrategy 中尝试膨胀布局文件 R.layout.tab_indicator ,该文件似乎不可用。
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);
是否有一种我不知道的为 hive 创建标签的新方法?我仔细阅读了文档,但没有看到任何表明我所做的有问题的东西。
在我们可以对 UI 小部件进行更全面的重组之前,我想避免在这一点上使用 fragment ,并且我想更好地理解这个问题。
最佳答案
我相信我已经找到了解决方案,但由于人们很好奇,这里是我遇到这个问题时得到的堆栈跟踪:
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 看到的:
spec = myTabHost.newTabSpec("score_game").setIndicator("Score", res.getDrawable(R.drawable.icon_field_gloss)).setContent(intent);
myTabHost.addTab(spec);
请注意,myTabHost
是一个 TabHost,而 spec
是一个 TabSpec。
以前,我是这样初始化 myTabHost
的:
//WRONG - This can CRASH your app starting at Android SDK 3.0
TabHost myTabHost = new TabHost(getContext());
为了解决这个问题,我开始通过这样做来初始化 TabHost:
TabHost myTabHost = new TabHost(getContext(), null);
然后就解决了!我很想找到根本原因,但我还没有弄清楚。
关于android - Honeycomb 和 TabHost 规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5408452/