【问题标题】:How can I use tabhost?如何使用标签主机?
【发布时间】:2012-01-24 21:53:07
【问题描述】:

我想使用标签,但它不起作用。 这是我的 .java 代码:

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

        TabSpec tab1 = tabHost.newTabSpec("tab1");
        TabSpec tab2 = tabHost.newTabSpec("tab2");
        TabSpec tab3 = tabHost.newTabSpec("tab3");
        TabSpec tab4 = tabHost.newTabSpec("tab4");
        TabSpec tab5 = tabHost.newTabSpec("tab5");
        tab1.setIndicator("menu").setContent(new Intent(this, tab1.class));
        tab2.setIndicator("language").setContent(new Intent(this,tab2.class ));
        tab3.setIndicator("speaking").setContent(new Intent(this,tab3.class));
        tab4.setIndicator("share").setContent(new Intent(this, tab4.class));
        tab5.setIndicator("winnig").setContent(new Intent(this,tab5.class ));
        tabHost.addTab(tab1);
        tabHost.addTab(tab2);
        tabHost.addTab(tab3);
        tabHost.addTab(tab4); 
        tabHost.addTab(tab5);

这里是我的 .xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost">

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

        <TabWidget android:id="@android:id/tabs"
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent"/>

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

这是错误:

> 01-24 23:48:47.048: E/AndroidRuntime(19837):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.tasarimprojesi.donem1/com.tasarimprojesi.donem1.user_manual}:
> java.lang.NullPointerException

【问题讨论】:

  • 请显示更多代码。你在findviewbyid 之前设置了内容视图吗?请发布完整的堆栈跟踪,并向我们展示它所指的行。
  • 发布更多错误堆栈跟踪。您发布的那个除了 NullPointerException 之外没有任何信息。
  • 这是我的主类 setContentView(R.layout.user_manual);这是 tab1.class 包 com.tasarimprojesi.donem1;导入android.app.Activity;导入android.os.Bundle;导入 android.widget.TextView;公共类 tab1 扩展 Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textView = new TextView(this); textView.setText("İkinci 标签");设置内容视图(文本视图); } }
  • 01-24 23:48:47.048: E/AndroidRuntime(19837): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659) 01-24 23:48:47.048: E/ AndroidRuntime(19837): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675) 01-24 23:48:47.048: E/AndroidRuntime(19837): 在 android.app.ActivityThread.access$1500(ActivityThread.java: 121)
  • 01-24 23:48:47.048: E/AndroidRuntime(19837): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:943) 01-24 23:48:47.048: E/AndroidRuntime(19837):在 android.os.Handler.dispatchMessage(Handler.java:99)

标签: android tabs android-tabhost


【解决方案1】:

你应该扩展 TabActivity,是吗?

然后

TabHost host = getTabHost();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多