Java 如何在对话框中的Tabhost中声明Numberpicker?
我想在Java 如何在对话框中的Tabhost中声明Numberpicker?,java,android,nullpointerexception,dialog,android-tabhost,Java,Android,Nullpointerexception,Dialog,Android Tabhost,我想在Tabhost中使用NumberPicker,它可以通过对话框访问。但是如何定义NumberPicker?目前我正在这样做: NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker); 但这是行不通的。我在下面的一行中得到一个NullPointerException(参见下面的代码) 有什么建议吗?或者怎样做才是正确的 String[] array = {"1","2","3"}; public
Tabhost
中使用NumberPicker
,它可以通过对话框访问。但是如何定义NumberPicker
?目前我正在这样做:
NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker);
但这是行不通的。我在下面的一行中得到一个NullPointerException(参见下面的代码)
有什么建议吗?或者怎样做才是正确的
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();
}
Logcat:
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)
<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>
<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>
<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>
Dialog.xml:
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)
<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>
<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>
<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:
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)
<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>
<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>
<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-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)
<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>
<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>
<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>
你初始化了你的数组吗?是的,让我补充一下..请发布dialog.xml文件,可能那里没有numberPicker,因此d.findViewById(R.id.numberPicker)返回nulldialog.xml
没有numberPicker
但它包含到其他.xml
有numberPicker的文件尝试以下操作:View tab=d.findviewbyd(R.id.tab1act);NumberPicker np=(NumberPicker)tab.findViewById(R.id.NumberPicker);并查看tab是null还是np。请注意,您将包含相同的布局(tab1act.xml)两次(调用相同的xml文件)。