我已经开始使用HelloTabwidget示例,它运行正常。现在我要做的是在每个标签内都有一个布局。这是代码的样子
myApp extends TabActivity {
TabHost tabHost;
onCreate() {
setContentView (R.layout.main);
tabHost = getTabHost();
TabHost.tabspec spec = tabHost.newTabSpec(..).setIndicator(...).
setContent(new Intent().setClass(...));
tabHost.addTab(spec);
:
}
}
myTab1Actvity extends Activity {
ListView myLV;
EditText myET;
onCreate() {
setcontentView (TAB1_LAYOUT);
myLV = findViewById(..);
:
myLV.setAdapter(aa);
myET.setOnKeyListener(....)
registerforContextMenu(myLV)
}
}
当我在这里运行时是堆栈跟踪
ERROR/AndroidRuntime(691): java.lang.NullPointerException
ERROR/AndroidRuntime(691): at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
ERROR/AndroidRuntime(691): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
ERROR/AndroidRuntime(691): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
ERROR/AndroidRuntime(691): at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
ERROR/AndroidRuntime(691): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
ERROR/AndroidRuntime(691): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
ERROR/AndroidRuntime(691): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
ERROR/AndroidRuntime(691): at android.view.ViewRoot.handleMessage(ViewRoot.java:1819)
ERROR/AndroidRuntime(691): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(691): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(691): at android.app.ActivityThread.main(ActivityThread.java:4363)
ERROR/AndroidRuntime(691): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(691): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(691): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
ERROR/AndroidRuntime(691): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
代码的流程看起来不错吗?基本上每个选项卡都有自己的活动,每个选项卡都有自己的布局。
TabHost似乎出现了问题 - 但我无法确定它。你有任何建议/ Qs将非常有帮助。
答案 0 :(得分:1)
让我想起“无限猴子定理” - 无论如何问题是每个标签的布局文件中还有一个TabHost。我使用的第一级(根)标签是LinearLayout(而不是TabHost)