【问题标题】:Android error with TabActivity / TabWidgetTabActivity / TabWidget的Android错误
【发布时间】:2010-12-26 16:53:57
【问题描述】:

我正在尝试为我的 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 行替换为以下内容,那么我会立即得到一个强制关闭窗口。

<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 以外的其他视图将标签内容组合在一起吗?

编辑:如果它与我的问题相关,我将扩展 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));
}

编辑:根据要求,这是堆栈跟踪,以及一条错误消息,只读“未找到源。”:

线程 [ main](已暂停 (异常运行时异常)) ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, 意图)行:2401 ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, 意图)行:2417 ActivityThread.access$2100(ActivityThread, ActivityThread$ActivityRecord, Intent) 线路:116 ActivityThread$H.handleMessage(消息) 线路:1794 ActivityThread$H(Handler).dispatchMessage(Message) 行:99 Looper.loop() 行:123 ActivityThread.main(String[]) 行: 第4203章 对象[],类,类[],类,整数, boolean) 行:不可用 [native 方法] Method.invoke(对象, 对象...)行:521 ZygoteInit$MethodAndArgsCaller.run() 行:791 ZygoteInit.main(字符串 []) 行:549 NativeStart.main(字符串 []) 行:不可用[本机方法]

【问题讨论】:

  • 堆栈跟踪是什么?可以发在这里吗?

标签: android layout views tabs


【解决方案1】:

哇,我是个白痴。在我的 LinearLayout 声明中,它们都包含 layout_height 属性而不是必需的 android:layout_height 属性。我的疏忽太愚蠢了,我不会再犯这种错误了。

感谢您花时间查看,DroidIn.net。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多