我想在NumberPicker
内使用Tabhost
,Dialog
可通过NumberPicker
访问。但是如何定义NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker);
?目前我正在这样做:
String[] array = {"1","2","3"};
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(R.id.tabHost);
tabHost.setup(getLocalActivityManager());
tabHost.setup();
TabHost.TabSpec tab1 = tabHost.newTabSpec("Favs");
TabHost.TabSpec tab2 = tabHost.newTabSpec("All");
tab1.setIndicator("Favs");
tab1.setContent(R.id.tab1act);
tab2.setIndicator("All");
tab2.setContent(R.id.tab2act);
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.setCurrentTab(0);
NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker);
np.setMaxValue(array.length); //NullPointerException here
np.setMinValue(1);
np.setWrapSelectorWheel(true);
np.setOnValueChangedListener(this);
np.setDisplayedValues(array);
np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
d.show();
}
但这不起作用。我在下面的行中得到一个NullPointerException(参见下面的代码)。
有什么建议吗?或者怎么做对了?
1-30 17:18:27.579 32038-32038/com.app.main E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.app.main.Main.dialog(Main.java:283)
at com.app.main.Main$1.onClick(Main.java:142)
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)
logcat的:
<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">
<include
android:id="@+id/tab1act"
android:layout_width="fill_parent"
android:layout_height="match_parent"
layout="@layout/tab1act" >
</include>
<include
android:id="@+id/tab2act"
android:layout_width="fill_parent"
android:layout_height="match_parent"
layout="@layout/tab1act" >
</include>
</FrameLayout>
</LinearLayout>
</TabHost>
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">
<NumberPicker
android:id="@+id/numberPicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Tab1act.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">
<NumberPicker
android:id="@+id/numberPicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Tab2act.xml:
{{1}}