Android 添加 TabHost 后应用程序崩溃

Android Application is crashing after adding TabHost

我正在制作一个应用程序,它可以通过观看教程将联系人添加到您的 phone 联系人列表,但是当我将标签添加到我的应用程序并 运行 它时,它崩溃并显示消息不幸的是我的应用程序已停止

这是我的主xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Contact_main"
android:orientation="vertical"
android:id="@+id/background">


<TabHost
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:id="@+id/tabhost"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false">

    <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"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"></TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="Contact Creators"
                    android:id="@+id/lblcreatortitle"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp"
                    android:textSize="30sp" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPersonName"
                    android:ems="10"
                    android:id="@+id/textname"
                    android:layout_marginTop="15dp"
                    android:hint="Contact name" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="phone"
                    android:ems="10"
                    android:id="@+id/textphone"
                    android:layout_gravity="center"
                    android:hint="Phone no"
                    android:focusableInTouchMode="true"
                    android:layout_marginTop="15dp"
                    android:editable="false" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textEmailAddress"
                    android:ems="10"
                    android:id="@+id/textemail"
                    android:layout_gravity="center_horizontal"
                    android:hint="Email"
                    android:focusableInTouchMode="true"
                    android:layout_marginTop="15dp" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:id="@+id/textaddress"
                    android:layout_gravity="center_horizontal"
                    android:hint="Address"
                    android:focusableInTouchMode="true"
                    android:layout_marginTop="15dp"
                    android:inputType="textPostalAddress" />

                <Button
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:text="Add Contact"
                    android:id="@+id/buttonadd"
                    android:layout_gravity="center"
                    android:layout_marginTop="15dp"
                    android:enabled="false" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"></LinearLayout>

        </FrameLayout>
    </LinearLayout>
</TabHost>

这是我的主要活动

package com.example.sayedshazeb.contact;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;


public class Contact_main extends ActionBarActivity {

EditText nametext,phonetext,emailtext,adresstext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_main);

    nametext = (EditText) findViewById(R.id.textname);
    phonetext = (EditText) findViewById(R.id.textphone);
    emailtext = (EditText) findViewById(R.id.textemail);
    adresstext = (EditText) findViewById(R.id.textaddress);
    TabHost tabHost = (TabHost)findViewById (R.id.tabhost);
    tabHost.setup();
    TabHost.TabSpec spec1 = tabHost.newTabSpec("Tab1");
    spec1.setContent(R.id.tab1);
    spec1.setIndicator("Contact");

    TabHost.TabSpec spec2 = tabHost.newTabSpec("Tab2");
    spec2.setContent(R.id.tab2);
    spec2.setIndicator("List");

    tabHost.addTab(spec1);
    tabHost.addTab(spec2);







    final Button addbtn = (Button) findViewById(R.id.buttonadd);

    nametext.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            addbtn.setEnabled(!nametext.getText().toString().trim().isEmpty());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

}

这是我的日志-

01-21 11:23:02.784    3064-3064/com.example.sayedshazeb.contact E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.sayedshazeb.contact, PID: 3064
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sayedshazeb.contact/com.example.sayedshazeb.contact.Contact_main}: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:212)
            at android.app.ActivityThread.main(ActivityThread.java:5137)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
            at android.widget.TabHost.addTab(TabHost.java:221)
            at com.example.sayedshazeb.contact.Contact_main.onCreate(Contact_main.java:36)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:212)
            at android.app.ActivityThread.main(ActivityThread.java:5137)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
            at dalvik.system.NativeStart.main(Native Method)

我不知道我做错了什么?为什么我的应用程序在添加选项卡后崩溃?

当您从 xml 绑定 TabHost 时,您似乎使用的是 android.R.id.tabhost 而不是 R.id.tabhost。没有一些 logcat 输出那是我最好的猜测。

祝你好运