[英]Why do I get an "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'" error?
I am new in Android studio development and errors like this have really messed up my day.我是 Android Studio 开发的新手,这样的错误真的让我的一天变得一团糟。 I am looking for a solution 2 days now many people had the same problem with me but each one of them had a very different solution.
我正在寻找解决方案 2 天,现在很多人都和我有同样的问题,但他们每个人都有非常不同的解决方案。 Even in Stackoverflow the solutions varied, so I would really appreciate if you could help me overpass this error so I can go on.
即使在 Stackoverflow 中,解决方案也各不相同,所以如果你能帮助我克服这个错误,我就可以继续下去了,我真的很感激。
The error I am getting is this:我得到的错误是这样的:
java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
java.lang.RuntimeException:您的 TabHost 必须有一个 TabWidget,其 id 属性为“android.R.id.tabs”
I have modified the .xml file and the .java file a lot with no chance.我在没有机会的情况下修改了 .xml 文件和 .java 文件。
Here is the .xml:这是.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>
and the .java:和.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);
}
} }
I would really appreciate any recommendations.我真的很感激任何建议。
Your error ( Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
) says it all:您的错误(
Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
)说明了一切:
Change this改变这个
<TabWidget
android:id="@+id/TabsHost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"></TabWidget>
to到
<TabWidget
android:id="@+id/android.R.id.tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</TabWidget>
Change this改变这个
<TabWidget
android:id="@+id/tabs"
</TabWidget>
to到
<TabWidget
android:id="@+id/android.R.id.tabs"
</TabWidget>
Change this改变这个
<TabWidget
android:id="@+id/tabs"
</TabWidget>
to到
<TabWidget
android:id="@android:id/tabs"
</TabWidget>
and same thing with franelayout maybe和 franlayout 一样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.