2012-01-23 67 views
0

我正在使用下面的代码在活动中创建三个选项卡,但当我使用addtab方法添加选项卡时,它会给我空指针异常。Android Tab选项卡

  tabHost.addTab(firstTabSpec);//Null Pointer error occured here 
      tabHost.addTab(secondTabSpec); 
      tabHost.addTab(thirdTabSpec); 

下面是我的代码都布局XML文件,并在那里创建标签,并把actvity它onCreaate()方法。

@Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      try { 
       /** TabHost will have Tabs */ 
       TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 

       /** 
       * TabSpec used to create a new tab. By using TabSpec only we can 
       * able to setContent to the tab. By using TabSpec setIndicator() we 
       * can set name to tab. 
       */ 

       /** tid1 is firstTabSpec Id. Its used to access outside. */ 
       TabSpec firstTabSpec = tabHost.newTabSpec("tid1"); 
       TabSpec secondTabSpec = tabHost.newTabSpec("tid1"); 
       TabSpec thirdTabSpec = tabHost.newTabSpec("tid1"); 

       /** TabSpec setIndicator() is used to set name for the tab. */ 
       /** 
       * TabSpec setContent() is used to set content for a particular tab. 
       */ 
       firstTabSpec.setIndicator("Update-1").setContent(
         new Intent(this, Keyboard.class)); 
       secondTabSpec.setIndicator("Update-2").setContent(
         new Intent(this, Sensors.class)); 
       thirdTabSpec.setIndicator("Update-3").setContent(
         new Intent(this, Relays.class)); 

       /** Add tabSpec to the TabHost to display. */ 
       tabHost.addTab(firstTabSpec);//Null Pointer error occured here 
       tabHost.addTab(secondTabSpec); 
       tabHost.addTab(thirdTabSpec); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 

Layout main.xml file code 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
<TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:id="@+id/LinearLayout01" 
     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" > 
     </TabWidget> 

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

</TabHost> 

编辑XML文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 



<TabHost 
    android:id="@+id/tabhost1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:id="@+id/LinearLayout01" 
     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" > 
     </TabWidget> 

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

</TabHost> 

</LinearLayout> 

的logcat:

java.lang.NullPointerException 
    at android.widget.TabHost.addTab(TabHost.java:221) 
    at com.android.embededconversation.EmbededConversation.onCreate(EmbededConversation.java:51) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
    at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:130) 
    at android.app.ActivityThread.main(ActivityThread.java:3687) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:507) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
    at dalvik.system.NativeStart.main(Native Method) 
+0

发布logcat。 –

+0

@LalitPoptani我发布了logcat。 – user861973

回答

2

得到tabhost这是错误的

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

你正试图从错误的资源中膨胀,并且tabHost始终保持为空。

将其更改为:

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

并确保您如果您使用TabActivity包括正确的R(项目的R,不android.R)

编辑

,获取TabHost的方法是,我从我写的应用程序添加了一个示例:

您的TabActivity

public class yourTabActivity extends TabActivity 
{ 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_tab_widget); 

     // fetch the TabHost 
     final TabHost tabHost = (TabHost) getTabHost(); 

     // create Tabs 
     tabHost.addTab(createTab(yourActivity.class, 
       "tag", "yourTabTitle", R.drawable.tv)); 
    } 

    private TabSpec createTab(final Class<?> intentClass, final String tag, final String title, final int drawable) 
    { 
     final Intent intent = new Intent().setClass(this, intentClass); 
     final View tab = LayoutInflater.from(getTabHost().getContext()).inflate(R.layout.tab, null); 
     ((TextView) tab.findViewById(R.id.tab_text)).setText(title); 
     ((ImageView) tab.findViewById(R.id.tab_icon)).setImageResource(drawable); 

     return getTabHost().newTabSpec(tag).setIndicator(tab).setContent(intent); 
    } 

} 

main_tab_widget.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

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

      <HorizontalScrollView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:scrollbars="none" > 

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

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

</LinearLayout> 

例如用于tab.xml。这实际上是一个自定义选项卡。

<?xml version="1.0" encoding="utf-8"?> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@drawable/tab_bg_selector" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:padding="5dp"> 

     <ImageView android:id="@+id/tab_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:scaleType="fitCenter" /> 

     <TextView android:id="@+id/tab_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:textStyle="bold" 
      android:gravity="center_horizontal" 
      android:textSize="10sp" 
      android:ellipsize="marquee" 
      android:textColor="@android:color/black" /> 
    </LinearLayout> 

这应包括你需要一个标签控件一切......

+0

编写像你这样的,XML必须是@ + ID/tabhost – lulumeya

+0

事实上,这是假设这是他如何在他的main.xml称为TabHost – Rotemmiz

+0

感谢您的答复。但尽管它给了我同样的错误。正如你所说,我做出了改变。在布局xml文件中添加@ + id/tabhost。 – user861973

0

你需要使用@ + ID/someid或使用tabActivity和getTabHost()

0

使用机器人:ID =

<TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 


<!-- other code for tab widget and Frame --> 

</TabHost> 
:在TabHost的xml属性 “@安卓ID/tabhost”

,并在源文件中使用:

TabHost tabHost = getTabHost(); 

,以获得TA bHost。

+0

有像getTabHost没有这样的方法(); – user861973

+0

在源文件类中扩展TabActivity而不是Activity。我认为你正在扩展Activity类,这就是为什么你没有得到getTabHost()方法。 –

0
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     // Your Code Here. 

    </TabHost> 

这是您的xml标签页。为此

TabHost tabHost = getTabHost(); 

在代码中获取tabhost引用。

TabSpec firstTabSpec = tabHost.newTabSpec("tid1") 
           .setIndicator("Update-1") 
           .setContent(new Intent(this, Keyboard.class)); 

应用此另一个选项卡也。

tabHost.addTab(firstTabSpec); 
相关问题