问题描述:

I'm trying to use a Tabhostin a Dialog so that the user can toggle inside the Dialog between to layouts. But I always get a NullPointerException. Any advice? or How to do it right?

Dialog Method:

 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)

网友答案:
TabHost tabHost = (TabHost) d.findViewById(R.id.tabHost);

id is case sensitive

相关阅读:
Top