2012-01-08 67 views
0

我试图在Android中实现tabhost。Android Tabhost不工作​​

Radio_Debug [Android Application] 
     DalvikVM[localhost:8604]  
      Thread [<3> main] (Suspended (exception NullPointerException)) 
       TabHost.dispatchWindowFocusChanged(boolean) line: 295 
       LinearLayout(ViewGroup).dispatchWindowFocusChanged(boolean) line: 664 
       FrameLayout(ViewGroup).dispatchWindowFocusChanged(boolean) line: 664  
       LinearLayout(ViewGroup).dispatchWindowFocusChanged(boolean) line: 664 
       PhoneWindow$DecorView(ViewGroup).dispatchWindowFocusChanged(boolean) line: 664 
       ViewRoot.handleMessage(Message) line: 1819 
       ViewRoot(Handler).dispatchMessage(Message) line: 99 
       Looper.loop() line: 123 
       ActivityThread.main(String[]) line: 4363  
       Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] 
       Method.invoke(Object, Object...) line: 521 
       ZygoteInit$MethodAndArgsCaller.run() line: 860 
       ZygoteInit.main(String[]) line: 618 
       NativeStart.main(String[]) line: not available [native method] 
      Thread [<17> Binder Thread #4] (Running)  
      Thread [<15> Binder Thread #3] (Running)  
      Thread [<13> Binder Thread #2] (Running)  
      Thread [<11> Binder Thread #1] (Running)  

:我已经设置它,但是当我运行页面时,我得到一个空指针异常不我的Android手机上显示tabhost控制,并生成以下堆栈跟踪方面做得正确eveything Java代码链接到XML页面代码如下:

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TabHost; 


public class tabs extends Activity{ 


    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabmain); 

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


    } 



} 

,其容纳tabhost控制是如下的xml文件布局:

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

      <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> 
       <TabWidget android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget> 

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

        <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab1"> 


        </LinearLayout> 
        <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab2"> 


        </LinearLayout> 
        <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab3"> 


        </LinearLayout> 
        <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab4"> 


        </LinearLayout> 

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




    </LinearLayout> 
+0

你在这个tabhost里面有任何视图吗? – kosa 2012-01-08 15:35:53

+0

是的,我已经尝试了4个文本视图,我仍然得到相同的错误。 – user615099 2012-01-08 18:59:08

回答

4

你永远不establi sh任何标签。您需要拨打newTabSpec()addTab()方法TabHost来设置选项卡,指向您的TabWidget的子项。 Here is a sample application证明了这一点。

+0

我认为你错过了这一点。即使使用我迄今为止的代码,它也不应该产生错误。我已经尝试过与标签和文本视图,我仍然得到相同的错误。你知道我可能会错过什么吗? – user615099 2012-01-08 18:58:36

+1

@ user615099:“即使使用我迄今为止的代码,它应该不会产生错误” - 如果您有一个空的“TabHost”,您将得到一个错误。 “你知道我可能会错过什么吗?” - 关闭我的头顶,不,这就是为什么我给你指出一个有效的样本,所以你可以尝试确定你的代码和样本之间有什么不同。 – CommonsWare 2012-01-08 20:14:29

+0

@CommonsWare你是男人:)你的小样本帮我弄清楚我的tabhost中的布局有什么问题.....非常感谢:) – 2012-01-25 00:19:22