Android TabHost.addTab ->空指针异常

发布于 2024-11-19 19:26:33 字数 1116 浏览 2 评论 0原文

这是我的代码:

    public class Main extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            TabHost tabHost = new TabHost(this);

            TabHost.TabSpec tab = tabHost.newTabSpec("tab1");
            tab.setIndicator("Tab 1");
            tab.setContent(new TabHost.TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    TextView tv = new TextView(Main.this);
                    tv.setText("tab 1 content");
                    return tv;
                }
            });

            tabHost.addTab(tab);

            setContentView(tabHost);
        }
    }

我收到此错误:

    [...]
    07-13 20:26:49.261: ERROR/AndroidRuntime(625): Caused by: java.lang.NullPointerException
    07-13 20:26:49.261: ERROR/AndroidRuntime(625):     at android.widget.TabHost.addTab(TabHost.java:206)
    07-13 20:26:49.261: ERROR/AndroidRuntime(625):     at test.test.Main.onCreate(Main.java:27)
    [...]

我需要通过代码执行此操作,但无法使用 XML。谁能帮我修复这个代码吗?

Here is my code:

    public class Main extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            TabHost tabHost = new TabHost(this);

            TabHost.TabSpec tab = tabHost.newTabSpec("tab1");
            tab.setIndicator("Tab 1");
            tab.setContent(new TabHost.TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    TextView tv = new TextView(Main.this);
                    tv.setText("tab 1 content");
                    return tv;
                }
            });

            tabHost.addTab(tab);

            setContentView(tabHost);
        }
    }

I get this error:

    [...]
    07-13 20:26:49.261: ERROR/AndroidRuntime(625): Caused by: java.lang.NullPointerException
    07-13 20:26:49.261: ERROR/AndroidRuntime(625):     at android.widget.TabHost.addTab(TabHost.java:206)
    07-13 20:26:49.261: ERROR/AndroidRuntime(625):     at test.test.Main.onCreate(Main.java:27)
    [...]

I need to do this by code and I can't use XML. Can anyone help me fix this code please ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝海 2024-11-26 19:26:33

对于可能想知道 TabActivity 是否已弃用的人,请参阅文档 表示当您不使用 TabActivity 时,您需要在添加选项卡之前调用 setup() 。

tabHost.setup();

For people who might wonder about TabActivity being deprecated the documentation says that you need to call setup() before adding tabs, when you don't use a TabActivity.

tabHost.setup();
や莫失莫忘 2024-11-26 19:26:33

您应该使用 TabActivity,它需要将相同的特殊布局设置为内容(请参阅 http://developer.android.com/resources/tutorials/views/hello-tabwidget.html)。如果您不能使用 xml,您应该从 java 代码构造相同的内容:

public class Main extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TabHost tabHost = new TabHost(this);
    tabHost.setId(android.R.id.tabhost);

    TabWidget widget = new TabWidget(this);
    widget.setId(android.R.id.tabs);

    FrameLayout content = new FrameLayout(this);
    content.setId(android.R.id.tabcontent);

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.addView(widget);
    layout.addView(content);

    tabHost.addView(layout);

    setContentView(tabHost);

    TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1");
    tab1.setIndicator("Tab 1");
    tab1.setContent(new TabHost.TabContentFactory() {
        @Override
        public View createTabContent(String tag) {
            TextView tv = new TextView(Main.this);
            tv.setText("tab 1 content");
            return tv;
        }
    });

    tabHost.addTab(tab1);

    TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2");
    tab2.setIndicator("Tab 2");
    tab2.setContent(new TabHost.TabContentFactory() {
        @Override
        public View createTabContent(String tag) {
            TextView tv = new TextView(Main.this);
            tv.setText("tab 2 content");
            return tv;
        }
    });

    tabHost.addTab(tab2);

    setContentView(tabHost);
}

}

You should use TabActivity, it needs same special layout to be set as content (see http://developer.android.com/resources/tutorials/views/hello-tabwidget.html). If you can not use xml you should construct the same content from java code:

public class Main extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TabHost tabHost = new TabHost(this);
    tabHost.setId(android.R.id.tabhost);

    TabWidget widget = new TabWidget(this);
    widget.setId(android.R.id.tabs);

    FrameLayout content = new FrameLayout(this);
    content.setId(android.R.id.tabcontent);

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.addView(widget);
    layout.addView(content);

    tabHost.addView(layout);

    setContentView(tabHost);

    TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1");
    tab1.setIndicator("Tab 1");
    tab1.setContent(new TabHost.TabContentFactory() {
        @Override
        public View createTabContent(String tag) {
            TextView tv = new TextView(Main.this);
            tv.setText("tab 1 content");
            return tv;
        }
    });

    tabHost.addTab(tab1);

    TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2");
    tab2.setIndicator("Tab 2");
    tab2.setContent(new TabHost.TabContentFactory() {
        @Override
        public View createTabContent(String tag) {
            TextView tv = new TextView(Main.this);
            tv.setText("tab 2 content");
            return tv;
        }
    });

    tabHost.addTab(tab2);

    setContentView(tabHost);
}

}

琴流音 2024-11-26 19:26:33

检查框架源代码中的 TabHost.addTab(...) 方法表明您的 TabWidget 尚不可用。 TabWidget 必须首先在代码中创建,或者由系统在创建布局时创建,并且必须具有 android.R.id.tabs 的 id。

Checking the method TabHost.addTab(...) in the framework source suggests that your TabWidget is not available yet. A TabWidget must be created in code first or by the system when creating a layout and must have an id of android.R.id.tabs.

北座城市 2024-11-26 19:26:33

您是否考虑过扩展 TabActivity,然后调用 getTabHost() 来获取TabHost 的实例?不确定您的目标是什么,但这可能值得一看。

Have you thought about extending TabActivity, and then calling getTabHost() to get the instance of the TabHost? Not sure what your objective is, but this might be worth looking at.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文