我正在尝试为我的 Android 应用程序创建一个 TabActivity。当我的布局 XML 如下所示以及我的 2 个选项卡的虚拟 TextView 内容时,一切看起来都很好。
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="3px">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px">
<TextView android:id="@+id/login_form" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="DUMMY LOGIN FORM" />
<TextView android:id="@+id/register_form" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="DUMMY REGISTER FORM" />
</FrameLayout>
</LinearLayout>
</TabHost>
我想用包含多个 View 元素的 LinearLayout 替换每个 TextView。但是,如果我尝试用类似下面的内容替换 TextView 行,那么我会立即得到一个 Force Close 窗口。
<LinearLayout android:id="@+id/login_form" android:layout_width="fill_parent" layout_height="fill_parent" android:orientation="vertical">
<TextView android:id="@+id/username_label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/username" />
</LinearLayout>
<LinearLayout android:id="@+id/register_form" android:layout_width="fill_parent" layout_height="fill_parent" android:orientation="vertical">
<TextView android:id="@+id/username_desired_label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/username_desired" />
</LinearLayout>
为什么这会导致我强制关闭?我是否应该使用 LinearLayout 以外的其他 View 来将我的选项卡内容组合在一起?
编辑:如果它与我的问题相关,我正在扩展 TabActivity
类,我的 onCreate
方法如下所示:
protected void onCreate(Bundle savedInstanceState) {
// display the login/register view
super.onCreate(savedInstanceState);
setContentView(R.layout.login_register);
// associate the tab content with each tab, and specify the tab headers
setDefaultTab(0);
TabHost tabs = getTabHost();
tabs.addTab(tabs.newTabSpec("login").setIndicator(getResources().getText(R.string.login)).setContent(R.id.login_form));
tabs.addTab(tabs.newTabSpec("register").setIndicator(getResources().getText(R.string.register)).setContent(R.id.register_form));
}
编辑:根据要求,这里是堆栈跟踪,以及仅显示“未找到源”的错误消息。
Thread [<3> main] (Suspended (exception RuntimeException)) ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2401 ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2417 ActivityThread.access$2100(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 116 ActivityThread$H.handleMessage(Message) line: 1794 ActivityThread$H(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 123 ActivityThread.main(String[]) line: 4203 Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] Method.invoke(Object, Object...) line: 521 ZygoteInit$MethodAndArgsCaller.run() line: 791 ZygoteInit.main(String[]) line: 549 NativeStart.main(String[]) line: not available [native method]
最佳答案
哇,我是个白痴。在我的 LinearLayout 声明中,它们都包含 layout_height
属性,而不是必需的 android:layout_height
属性。我的愚蠢疏忽,我不会再犯同样的错误。
感谢您花时间浏览 DroidIn.net。
关于TabActivity/TabWidget 的 Android 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1905496/