Android 使用TabHost时出错
我刚刚编写了一个在Android中使用TabHost的示例代码,但它导致了错误 您的内容必须有一个id属性为“android.R.id.TabHost”的TabHost 我不知道我错在哪里 这是我的日志Android 使用TabHost时出错,android,android-tabhost,Android,Android Tabhost,我刚刚编写了一个在Android中使用TabHost的示例代码,但它导致了错误 您的内容必须有一个id属性为“android.R.id.TabHost”的TabHost 我不知道我错在哪里 这是我的日志 01-10 11:13:42.700: D/AndroidRuntime(4062): Shutting down VM 01-10 11:13:42.710: W/dalvikvm(4062): threadid=1: thread exiting with uncaught exceptio
01-10 11:13:42.700: D/AndroidRuntime(4062): Shutting down VM
01-10 11:13:42.710: W/dalvikvm(4062): threadid=1: thread exiting with uncaught exception (group=0x40a591f8)
01-10 11:13:42.730: E/AndroidRuntime(4062): FATAL EXCEPTION: main
01-10 11:13:42.730: E/AndroidRuntime(4062): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.irctcdemo/com.example.irctcdemo.TabBar}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
01-10 11:13:42.730: E/AndroidRuntime(4062): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-10 11:13:42.730: E/AndroidRuntime(4062): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-10 11:13:42.730: E/AndroidRuntime(4062): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-10 11:13:42.730: E/AndroidRuntime(4062): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-10 11:13:42.730: E/AndroidRuntime(4062): at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 11:13:42.730: E/AndroidRuntime(4062): at android.os.Looper.loop(Looper.java:137)
01-10 11:13:42.730: E/AndroidRuntime(4062): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-10 11:13:42.730: E/AndroidRuntime(4062): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 11:13:42.730: E/AndroidRuntime(4062): at java.lang.reflect.Method.invoke(Method.java:511)
01-10 11:13:42.730: E/AndroidRuntime(4062): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-10 11:13:42.730: E/AndroidRuntime(4062): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-10 11:13:42.730: E/AndroidRuntime(4062): at dalvik.system.NativeStart.main(Native Method)
01-10 11:13:42.730: E/AndroidRuntime(4062): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
01-10 11:13:42.730: E/AndroidRuntime(4062): at android.app.TabActivity.onContentChanged(TabActivity.java:131)
01-10 11:13:42.730: E/AndroidRuntime(4062): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
01-10 11:13:42.730: E/AndroidRuntime(4062): at android.app.Activity.setContentView(Activity.java:1835)
01-10 11:13:42.730: E/AndroidRuntime(4062): at com.example.irctcdemo.TabBar.onCreate(TabBar.java:20)
01-10 11:13:42.730: E/AndroidRuntime(4062): at android.app.Activity.performCreate(Activity.java:4465)
01-10 11:13:42.730: E/AndroidRuntime(4062): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-10 11:13:42.730: E/AndroidRuntime(4062): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
TabBar.java
TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.firstscreen);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
// Get TabHost Refference1
tabHost = getTabHost();
// Set TabChangeListener called when tab changed
tabHost.setOnTabChangedListener(this);
TabHost.TabSpec spec;
Intent intent;
/************* TAB1 ************/
// Create Intents to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Tab1Activity.class);
spec = tabHost.newTabSpec("First").setIndicator("")
.setContent(intent);
//Add intent to tab
tabHost.addTab(spec);
/************* TAB2 ************/
intent = new Intent().setClass(this, Tab2Activity.class);
spec = tabHost.newTabSpec("Second").setIndicator("")
.setContent(intent);
tabHost.addTab(spec);
// Set drawable images to tab
//tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab2);
//tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab3);
// Set Tab1 as Default tab and change image
tabHost.getTabWidget().setCurrentTab(0);
//tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab1_over);
}
firstscreen.xml代码
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tabhost">
<LinearLayout
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="@+id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="@+id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
您的活动应该扩展TabActivity而不是activity
public class irctcdemo extends TabActivity {
只要使用
tabHost = getTabHost();
去掉这个
tabHost = (TabHost)findViewById(android.R.id.tabhost);
对于tabhost
,您的Id应该是android:Id=“@android:Id/tabhost”
,并且
android:id=“@android:id/tabs”
对于TabWidget
将TabWidget id作为
android:id="@android:id/tabs"
和框架布局Id为
android:id="@android:id/tabcontent"
在您的xml文件中。当您获得tabHost引用时,似乎覆盖了之前对tabHost tabHost=(tabHost)findViewById(android.R.id.tabHost)的赋值;不需要此行,只需使用tabHost=getTabHost()@PadmaKumar:我也检查了这个案例,但它会导致相同的错误。您是否在
R.layout.firstscreen
中指定了TabHost
,发布您的firstscreen.xml布局。我已经通过TabActivity扩展了它,删除了该行,然后也得到了相同的错误。这两种语法是否有任何差异?在您需要时注意差异它将查找android ID。如果扩展活动并声明为自己的id,则需要调用tabHost=(tabHost)findViewById(R.id.my\u tabHost\u id);