设置 NumberPicker 变量的 setMinValue 时出现空指针异常
Null pointer exception when setting a NumberPicker variable's setMinValue
我正在尝试开发一个具有 textview 和 numberpicker 的对话。当我尝试设置数字选择器的最小值时出现异常。对话代码如下:
if ( v== text_mess_limit || v == text_mess_limit_text)
{
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.settings_number_picker_dialogue);
dialog.setTitle("Text Messages limit");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Set number of messages to save");
np = (NumberPicker) findViewById(R.id.numberPicker1);
np.setMinValue(0);
np.setMaxValue(10);
np.setWrapSelectorWheel(false);
np.setOnValueChangedListener(this);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonCancel);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
logcat的报告如下:
11-22 11:33:56.184: E/AndroidRuntime(23521): java.lang.NullPointerException
11-22 11:33:56.184: E/AndroidRuntime(23521): at com.commlink.messaging.Settings_Activity.onClick(Settings_Activity.java:143)
11-22 11:33:56.184: E/AndroidRuntime(23521): at android.view.View.performClick(View.java:4471)
11-22 11:33:56.184: E/AndroidRuntime(23521): at android.view.View$PerformClick.run(View.java:18784)
11-22 11:33:56.184: E/AndroidRuntime(23521): at android.os.Handler.handleCallback(Handler.java:808)
11-22 11:33:56.184: E/AndroidRuntime(23521): at android.os.Handler.dispatchMessage(Handler.java:103)
11-22 11:33:56.184: E/AndroidRuntime(23521): at android.os.Looper.loop(Looper.java:193)
11-22 11:33:56.184: E/AndroidRuntime(23521): at android.app.ActivityThread.main(ActivityThread.java:5330)
11-22 11:33:56.184: E/AndroidRuntime(23521): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 11:33:56.184: E/AndroidRuntime(23521): at java.lang.reflect.Method.invoke(Method.java:515)
11-22 11:33:56.184: E/AndroidRuntime(23521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
11-22 11:33:56.184: E/AndroidRuntime(23521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
11-22 11:33:56.184: E/AndroidRuntime(23521): at dalvik.system.NativeStart.main(Native Method)
Settings_Activity.java:143行如下:
np.setMinValue(0);
为什么会出现此错误?我该如何解决这个问题?
a dialogue having a textview and a numberpicker
然后访问NumberPicker
视图,方法与从Dialog
访问TextView
相同。
改变
np = (NumberPicker) findViewById(R.id.numberPicker1);
至
np = (NumberPicker) dialog. findViewById(R.id.numberPicker1);
我正在尝试开发一个具有 textview 和 numberpicker 的对话。当我尝试设置数字选择器的最小值时出现异常。对话代码如下:
if ( v== text_mess_limit || v == text_mess_limit_text)
{
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.settings_number_picker_dialogue);
dialog.setTitle("Text Messages limit");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Set number of messages to save");
np = (NumberPicker) findViewById(R.id.numberPicker1);
np.setMinValue(0);
np.setMaxValue(10);
np.setWrapSelectorWheel(false);
np.setOnValueChangedListener(this);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonCancel);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
logcat的报告如下:
11-22 11:33:56.184: E/AndroidRuntime(23521): java.lang.NullPointerException
11-22 11:33:56.184: E/AndroidRuntime(23521): at com.commlink.messaging.Settings_Activity.onClick(Settings_Activity.java:143)
11-22 11:33:56.184: E/AndroidRuntime(23521): at android.view.View.performClick(View.java:4471)
11-22 11:33:56.184: E/AndroidRuntime(23521): at android.view.View$PerformClick.run(View.java:18784)
11-22 11:33:56.184: E/AndroidRuntime(23521): at android.os.Handler.handleCallback(Handler.java:808)
11-22 11:33:56.184: E/AndroidRuntime(23521): at android.os.Handler.dispatchMessage(Handler.java:103)
11-22 11:33:56.184: E/AndroidRuntime(23521): at android.os.Looper.loop(Looper.java:193)
11-22 11:33:56.184: E/AndroidRuntime(23521): at android.app.ActivityThread.main(ActivityThread.java:5330)
11-22 11:33:56.184: E/AndroidRuntime(23521): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 11:33:56.184: E/AndroidRuntime(23521): at java.lang.reflect.Method.invoke(Method.java:515)
11-22 11:33:56.184: E/AndroidRuntime(23521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
11-22 11:33:56.184: E/AndroidRuntime(23521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
11-22 11:33:56.184: E/AndroidRuntime(23521): at dalvik.system.NativeStart.main(Native Method)
Settings_Activity.java:143行如下:
np.setMinValue(0);
为什么会出现此错误?我该如何解决这个问题?
a dialogue having a textview and a numberpicker
然后访问NumberPicker
视图,方法与从Dialog
访问TextView
相同。
改变
np = (NumberPicker) findViewById(R.id.numberPicker1);
至
np = (NumberPicker) dialog. findViewById(R.id.numberPicker1);