2012-04-11 114 views
1

搜索解决方案后,我决定重新提出一个问题already asked hereLinearLayout中的android TabHost

我有一个TabHost控件,我需要把它放在其他布局(LinearLayout)中。我整理了谷歌给我们的例子(http://developer.android.com/resources/tutorials/views/hello-tabwidget.html),并做了同样的事情,唯一的区别是TabHost不是布局的根,显示视图的活动类没有扩展TabActivity,但活动

我的XML是这样的:

<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:orientation="vertical" 
      android:layout_width="fill_parent" android:layout_height="fill_parent" 
      android:padding="5dp"> 
      <TabWidget android:id="@android:id/tabs" 
       android:layout_width="fill_parent" android:layout_height="53px" 
       android:tabStripEnabled="false" android:layout_margin="1px" 
       android:background="#222222" /> 
      <FrameLayout android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" android:layout_height="fill_parent" 
       android:padding="5dp" /> 
     </LinearLayout> 
    </TabHost> 
</LinearLayout> 

和Java类是这样的:

public class TabsExampleActivity extends Activity { 

    TabHost tabHost; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     initTabs(); 

    } 


    private void initTabs() { 
     Resources res = getResources();      // Resource object to get Drawables 
     tabHost = (TabHost) findViewById(R.id.tabhost);  // The activity TabHost 

     TabHost.TabSpec spec;        // Resusable TabSpec for each tab 
     Intent intent;          // Reusable Intent for each tab   

     intent = new Intent("eu.pkg.tabsExample.BuildingTabActivity"); 
     spec = tabHost.newTabSpec("text").setIndicator("", 
          res.getDrawable(R.drawable.tab_text_selector)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, ClipArtTabActivity.class); 
     spec = tabHost.newTabSpec("clipart").setIndicator("", 
          res.getDrawable(R.drawable.tab_clipart_selector)) 
         .setContent(intent); 
     tabHost.addTab(spec); 


     intent = new Intent().setClass(this, FrameSelectorTabActivity.class); 
     spec = tabHost.newTabSpec("frame").setIndicator("", 
          res.getDrawable(R.drawable.tab_frame_selector)) 
         .setContent(intent); 
     tabHost.addTab(spec); 



     intent = new Intent().setClass(this, BgSelectorTabActivity.class); 
     spec = tabHost.newTabSpec("bg").setIndicator("", 
          res.getDrawable(R.drawable.tab_bg_selector)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, BuildingTabActivity.class); 
     spec = tabHost.newTabSpec("build").setIndicator("", 
          res.getDrawable(R.drawable.tab_build_selector)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(4); 
    } 
} 

如果我作为基地活动TabActivity使用,我用的是TabHost为根,然后将代码工作得很好,我看标签......等。

这个问题似乎是仅当余延伸活性和错误是:

04-11 18:53:38.874:ERROR/AndroidRuntime(32539): 了java.lang.RuntimeException:无法启动活动 ComponentInfo {eu.pkg.tabsExample/eu.pkg.tabsExample.TabsExampleActivity}: 显示java.lang.NullPointerException

的问题是:如何举办的LinearLayout内TabHost?

编辑:

在意见建议改变后,日志猫错误是:

04-11 20:52:01.993:ERROR/imdg81(28747):IsShutDownStarted() 04-11 20:52:01.999:错误/ SensorManager(28747):registerListener 4:gp2a 光传感器延迟:240 04-11 20:52:01.999:错误/ SensorManager(28747): ====== = >>> Sensor Thread RUNNING < < < ======== 04-11 20:52:02.075:ERROR/i mdg81(28747):IsShutDownStarted()04-11 20:52:02.284: 错误/ LockPatternKeyguardView(28747):电话启动完成。所以可以 发送广播消息用于音乐播放器04-11 20:52:05.507: ERROR/JDWP(3675):无法写入握手字节:断管(-1 14)04-11 20:52:05.609:ERROR/AndroidRuntime(3675):致命异常: main 04-11 20:52:05.609:ERROR/AndroidRuntime(3675): java.lang.RuntimeException:无法启动活动 ComponentInfo {eu.pkg.tabsExample/eu.pkg .tabsExample.TabsExampleActivity}: java.lang.IllegalStateException:您是否忘记调用'public void setup(LocalActivityManager activityGroup)'? 04-11 20:52:05.609: 错误/ AndroidRuntime(3675):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 04-11 20:52:05.609:ERROR/AndroidRuntime(3675):在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 04-11 20:52:05.609:ERROR/AndroidRuntime(3675):at android.app.ActivityThread.access $ 2300(ActivityThread.java:125) 04-11 20:52:05.609:ERROR/AndroidRuntime(3675):at android.app.ActivityThread $ H.handleMessage(ActivityThread。java:2033) 04-11 20:52:05.609:ERROR/AndroidRuntime(3675):at android.os.Handler.dispatchMessage(Handler.java:99)04-11 20:52:05.609:ERROR/AndroidRuntime (3675):at android.os.Looper.loop(Looper.java:123)04-11 20:52:05.609: ERROR/AndroidRuntime(3675):at android.app.ActivityThread.main(ActivityThread.java :4627)04-11 20:52:05.609:ERROR/AndroidRuntime(3675):at java.lang.reflect.Method.invokeNative(Native Method)04-11 20:52:05.609:ERROR/AndroidRuntime(3675) ): java.lang.reflect.Method.invoke(Method.java:521)04-11 20:52:05.609: ERROR/AndroidRuntime(3675):at com.android.internal.os.ZygoteInit $ Method AndArgsCaller.run(ZygoteInit.java:858) 04-11 20:52:05.609:ERROR/AndroidRuntime(3675):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)04-11 20:52:05.609:ERROR/AndroidRuntime(3675):at dalvik.system.NativeStart.main(Native Method)04-11 20:52:05.609: 错误/ AndroidRuntime(3675):原因: java。 lang.IllegalStateException:您忘记调用'public void setup(LocalActivityManager activityGroup)'吗? 04-11 20:52:05.609: 错误/ AndroidRuntime(3675):在 android.widget.TabHost $ IntentContentStrategy.getContentView(TabHost.java:654) 04-11 20:52:05.609:ERROR/AndroidRuntime(3675) ):at android.widget.TabHost.setCurrentTab(TabHost.java:328)04-11 20:52:05.609:ERROR/AndroidRuntime(3675):at android.widget.TabHost.addTab(TabHost.java:213 )04-11 20:52:05.609: ERROR/AndroidRuntime(3675):at eu.pkg.tabsExample.TabsExampleActivity.initTabs(TabsExampleActivity.java:43) 04-11 20:52:05.609:ERROR/AndroidRuntime( 3675):at eu.pkg.tabsExample.TabsExampleActivity.onCreate(TabsExampleActivity.java:27) 04-11 20:52:05.609:ERROR/AndroidRuntime(3675):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-11 20:52:05.609:ERROR/AndroidRuntime(3675):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 04 -11 20:52:05.609:ERROR/AndroidRuntime(3675):... 11多个

+2

如果不扩展TabActivity,则无法创建Tab。 – kosa 2012-04-11 15:59:42

+0

你确定吗?你从哪里得到这些信息?还有其他布局可以存在的TabHost替代方案吗? – 2012-04-11 16:05:01

+0

让线性布局中的tabhost不是问题(我正在做同样的事情),但没有扩展tabactivity,你不能创建Tab。 – kosa 2012-04-11 16:07:03

回答

6

问题解决了。 正确的解决方案,为用户207的评论,从另一个计算器问题的一个给定的是这样的:

这是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="@android:id/tabhost" 
     android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     <LinearLayout android:orientation="vertical" 
      android:layout_width="fill_parent" android:layout_height="fill_parent" 
      android:padding="5dp"> 
      <TabWidget android:id="@android:id/tabs" 
       android:layout_width="fill_parent" android:layout_height="53px" 
       android:tabStripEnabled="false" android:layout_margin="1px" 
       android:background="#222222" /> 
      <FrameLayout android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" android:layout_height="fill_parent" 
       android:padding="5dp" /> 
     </LinearLayout> 
    </TabHost> 
</LinearLayout> 

和java文件:

package eu.pkg.tabsExample; 

import android.app.Activity; 
import android.app.LocalActivityManager; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.os.Handler; 
import android.widget.TabHost; 

public class TabsExampleActivity extends Activity { 

    TabHost tabHost; 
    LocalActivityManager mLocalActivityManager; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     initTabs(savedInstanceState); 
    } 

    @Override 
    protected void onResume() { 
     mLocalActivityManager.dispatchResume(); 
     super.onResume(); 
    } 

    @Override 
    protected void onPause() { 
     mLocalActivityManager.dispatchPause(isFinishing()); 
     super.onPause(); 
    } 

    private void initTabs(Bundle savedInstanceState) { 
     Resources res = getResources();      // Resource object to get Drawables 
     tabHost = (TabHost) findViewById(android.R.id.tabhost);  // The activity TabHost 
     mLocalActivityManager = new LocalActivityManager(this, false); 
     mLocalActivityManager.dispatchCreate(savedInstanceState); 
     tabHost.setup(mLocalActivityManager); 

     TabHost.TabSpec spec;        // Resusable TabSpec for each tab 
     Intent intent;          // Reusable Intent for each tab   

     intent = new Intent("eu.pkg.tabsExample.BuildingTabActivity"); 
     spec = tabHost.newTabSpec("text").setIndicator("", 
          res.getDrawable(R.drawable.tab_text_selector)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, ClipArtTabActivity.class); 
     spec = tabHost.newTabSpec("clipart").setIndicator("", 
          res.getDrawable(R.drawable.tab_clipart_selector)) 
         .setContent(intent); 
     tabHost.addTab(spec); 


     intent = new Intent().setClass(this, FrameSelectorTabActivity.class); 
     spec = tabHost.newTabSpec("frame").setIndicator("", 
          res.getDrawable(R.drawable.tab_frame_selector)) 
         .setContent(intent); 
     tabHost.addTab(spec); 



     intent = new Intent().setClass(this, BgSelectorTabActivity.class); 
     spec = tabHost.newTabSpec("bg").setIndicator("", 
          res.getDrawable(R.drawable.tab_bg_selector)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, BuildingTabActivity.class); 
     spec = tabHost.newTabSpec("build").setIndicator("", 
          res.getDrawable(R.drawable.tab_build_selector)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(4); 
    } 
} 

幕后神奇的是在这些4行代码:

tabHost = (TabHost) findViewById(android.R.id.tabhost);  // The activity TabHost 
    mLocalActivityManager = new LocalActivityManager(this, false); 
    mLocalActivityManager.dispatchCreate(savedInstanceState); 
    tabHost.setup(mLocalActivityManager); 

谢谢大家对这个答案的贡献。

0

更改此特定行

TabHost机器人:ID = “@ + ID/tabhost”

TabHost机器人:ID = “@机器人:ID/tabhost”

编辑

还添加后面的行

tabHost = (TabHost) findViewById(R.id.tabhost); 
tabHost.setup(); 
+0

我现在改变这样的: \t 和像Java这个:\t private void initTabs(){ Resources res = getResources(); \t \t \t \t \t //获取Drawables的资源对象 tabHost =(TabHost)findViewById(android.R.id.tabhost); \t \t //活动TabHost tabHost.setup(); TabHost。TabSpec规范; \t \t \t \t \t \t \t \t // Resusable则tabspec为每个标签 意图意图; \t \t \t \t \t \t \t \t \t //为每个标签可重复使用的 意向/// .........代码 – 2012-04-11 17:41:18

+0

的其余部分,仍然得到了空指针异常! – 2012-04-11 17:41:37

+0

我雾化了什么? – 2012-04-11 17:41:48

0

This将解决您的错误。

另外:我刚刚读到,你想从Activity而不是TabActivity扩展,因为你试图实现“嵌套选项卡”。 TabActivityTabActivity之内应该不成问题。之前没有做过,但我没有看到任何可能出现的麻烦。将ActivityTabActivity作为标签内容添加没有区别。你可以试一试