为什么我会得到“你的TabHost必须有一个TabWidget,其id属性是'android.R.id.tabs'”错误?

时间:2015-09-30 09:16:08

标签: java android

我是Android工作室开发的新手,这样的错误让我的一天搞得一团糟。我正在寻找一个解决方案2天,现在很多人遇到了同样的问题,但每个人都有一个非常不同的解决方案。即使在Stackoverflow中,解决方案也各不相同,所以如果你能帮我解决这个错误,我真的很感激,所以我可以继续。

我得到的错误是:

  

java.lang.RuntimeException:你的TabHost必须有一个TabWidget,其id属性是'android.R.id.tabs'

我已经很好地修改了.xml文件和.java文件。

这是.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" 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=".MainActivity"
android:clickable="true">


<TabHost
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@id/tabHost"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@+id/TabsHost"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"></TabWidget>

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

            <LinearLayout
                android:id="@+id/creatortab"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="Contact Creator"
                    android:id="@+id/ContactCreator"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true" />

                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="textPersonName"
                    android:text="Name"
                    android:ems="10"
                    android:id="@+id/name"
                    android:layout_below="@+id/ContactCreator"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="34dp"
                    android:layout_alignParentEnd="true"
                    android:hint="name" />

                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="phone"
                    android:ems="10"
                    android:id="@+id/phone"
                    android:layout_below="@+id/name"
                    android:layout_alignParentStart="true"
                    android:layout_alignEnd="@+id/name"
                    android:hint="Phone"
                    android:text="Phone"
                    android:layout_marginTop="44dp" />

                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="textEmailAddress"
                    android:ems="10"
                    android:id="@+id/email"
                    android:layout_below="@+id/phone"
                    android:layout_alignParentStart="true"
                    android:layout_alignEnd="@+id/phone"
                    android:text="E-mail"
                    android:layout_marginTop="54dp"
                    android:hint="email" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Add Contact"
                    android:id="@+id/addcontact"
                    android:enabled="false"
                    android:allowUndo="false"
                    android:clickable="false"
                    android:hint="add contact"
                    android:nestedScrollingEnabled="false"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="74dp" />

                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="textPostalAddress"
                    android:ems="10"
                    android:id="@+id/address"
                    android:text="Address"
                    android:layout_above="@+id/addcontact"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="64dp"
                    android:layout_alignEnd="@+id/email"
                    android:hint="address" />
            </LinearLayout>

               <LinearLayout
                    android:id="@+id/ListTab"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"></LinearLayout>

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

</RelativeLayout>

和.java:

EditText nameTxt, phoneTxt, emailTxt, addressTxt;


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

    nameTxt = (EditText)findViewById(R.id.name);
    phoneTxt = (EditText)findViewById(R.id.phone);
    emailTxt = (EditText)findViewById(R.id.email);
    addressTxt = (EditText)findViewById(R.id.address);

    TabHost tabHost = (TabHost) findViewById(R.id.tabHost);

    tabHost.setup();

    TabHost.TabSpec tabSpec = tabHost.newTabSpec("creator");
    tabSpec.setContent(R.id.creatortab);
    tabSpec.setIndicator("Creator");
    tabHost.addTab(tabSpec);

    tabSpec = tabHost.newTabSpec("list");
    tabSpec.setContent(R.id.ListTab);
    tabSpec.setIndicator("List");
    tabHost.addTab(tabSpec);



    final Button addBtn = (Button)findViewById(R.id.addcontact);
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Your contact has been added", Toast.LENGTH_SHORT).show();
        }
    });


    nameTxt.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(!nameTxt.getText().toString().trim().isEmpty());//if nameTxt is equals to nothing,
                                                                                //to trim koitaei gia kena pisw kai mprosta

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

我真的很感激任何建议。

3 个答案:

答案 0 :(得分:1)

您的错误(Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs')说明了一切:

更改此

<TabWidget
        android:id="@+id/TabsHost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"></TabWidget>

<TabWidget
        android:id="@+id/android.R.id.tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
</TabWidget>

答案 1 :(得分:0)

更改此

<TabWidget
android:id="@+id/tabs"
</TabWidget>

<TabWidget
android:id="@+id/android.R.id.tabs"
</TabWidget>

答案 2 :(得分:0)

改变这个

 <TabWidget
 android:id="@+id/tabs"
 </TabWidget>     

<TabWidget
android:id="@android:id/tabs"
</TabWidget>        

也许和 franlayout 一样