我正在尝试在Tabhost
中使用Dialog
,以便用户可以在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)
答案 0 :(得分:1)
TabHost tabHost = (TabHost) d.findViewById(R.id.tabHost);
id区分大小写