【问题标题】:NullPointerException using TabHost in Dialog? How to do it right?NullPointerException 在对话框中使用 TabHost?怎么做才对?
【发布时间】:2014-11-30 14:01:07
【问题描述】:

我正在尝试在Dialog 中使用Tabhost,以便用户可以在Dialog 内部切换到布局。但我总是收到NullPointerException。有什么建议吗?或如何正确操作?

对话方法:

    public void dialog(){

    final Dialog d = new Dialog(this);
    d.setTitle("Dialog);
    d.setContentView(R.layout.dialog);
    d.setCanceledOnTouchOutside(false);

    TabHost tabHost = (TabHost) d.findViewById(android.R.id.tabhost);
    Button b_set = (Button) d.findViewById(R.id.b_choose);
    Button b_cancel = (Button) d.findViewById(R.id.b_cancel);

    tabHost.setup();     //Here is the problem

    TabHost.TabSpec tab1 = tabHost.newTabSpec("Favs");
    TabHost.TabSpec tab2 = tabHost.newTabSpec("All");

    tab1.setIndicator("NEWTAB");
    tab1.setContent(new Intent(this,Tab1Activity.class));

    tab2.setIndicator("NEWTAB");
    tab2.setContent(new Intent(this,Tab2Activity.class));

    tabHost.addTab(tab1);
    tabHost.addTab(tab2);

    d.show();
    }

Dialog.xml:

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TabHost
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tabHost">

    <LinearLayout
        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:

         11-30 14:48:06.661    7157-7157/com.test.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
        java.lang.NullPointerException
        at com.test.app.Main.dialog (Main.java:269)
        at com.test.app.Main$1.onClick(Main.java:139)
        at android.view.View.performClick(View.java:4475)
        at android.view.View$PerformClick.run(View.java:18786)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

    标签: java android nullpointerexception dialog android-tabhost


    【解决方案1】:
    TabHost tabHost = (TabHost) d.findViewById(R.id.tabHost);
    

    id 区分大小写

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      相关资源
      最近更新 更多