TabActivity/TabWidget 的 Android 错误

标签 android layout view tabs

我正在尝试为我的 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/

相关文章:

android - 通过 AIDL 从远程服务到应用程序的复杂类型

android - 如何让三星时刻与亚行连接

html - 新的BFC "clearing" float 框

redirect - Django : At the view level, 我可以将 'noindex' header 添加到 'redirect' 响应中吗?

android - LayoutInflater 用于使用 xml 和 View 类

Android Studio - 无法通知项目评估监听器错误

android - 具有 ViewHolder 模式的 ListView 中的远程图像

python - PySide (Qt) - 布局不起作用

html - 同时使用 `position: absolute` 和 `float: left` 时的预期行为是什么?

android - 如何将 View 添加到 XML 布局 android