我正在尝试将2个数字选择器转换为字符串,但它似乎永远不会起作用。
我只是不断获得NPE,似乎无法弄清楚原因。
请参阅下面的代码和Logcat,
感谢任何帮助。
以下是onCreate .....
final Context context = this;
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.custominout, null);
AlertDialog.Builder renamedialog = new AlertDialog.Builder(context);
renamedialog.setTitle("Custom output change");
renamedialog.setView(promptsView);
int minInValue = 1;
int maxInValue = 16;
int currentInValue = 1;
final NumberPicker inCapacity = (NumberPicker) promptsView.findViewById(R.id.inpicker);
inCapacity.setMinValue(minInValue);
inCapacity.setMaxValue(maxInValue);
inCapacity.setValue(currentInValue);
int minOutValue = 1;
int maxOutValue = 16;
int currentOutValue = 1;
final NumberPicker outCapacity = (NumberPicker) promptsView.findViewById(R.id.outpicker);
outCapacity.setMinValue(minOutValue);
outCapacity.setMaxValue(maxOutValue);
outCapacity.setValue(currentOutValue);
renamedialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
whatin = (NumberPicker) findViewById(R.id.inpicker);
whatout = (NumberPicker) findViewById(R.id.outpicker);
String inpicker = String.valueOf(whatin.getValue());
String outpicker = String.valueOf(whatout.getValue());
try {
CustomInOut cus = new CustomInOut();
cus.setInpicker(inpicker);
cus.setOutpicker(outpicker);
cus.execute();
} catch(IllegalStateException e) {}
}
});
renamedialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
logcat的
01-04 11:58:39.278: E/AndroidRuntime(1432): FATAL EXCEPTION: main
01-04 11:58:39.278: E/AndroidRuntime(1432): java.lang.NullPointerException
01-04 11:58:39.278: E/AndroidRuntime(1432): at com.smarte.smartipcontrol.IPControl$1.onClick(IPControl.java:98)
01-04 11:58:39.278: E/AndroidRuntime(1432): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
01-04 11:58:39.278: E/AndroidRuntime(1432): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 11:58:39.278: E/AndroidRuntime(1432): at android.os.Looper.loop(Looper.java:137)
01-04 11:58:39.278: E/AndroidRuntime(1432): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-04 11:58:39.278: E/AndroidRuntime(1432): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 11:58:39.278: E/AndroidRuntime(1432): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 11:58:39.278: E/AndroidRuntime(1432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-04 11:58:39.278: E/AndroidRuntime(1432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-04 11:58:39.278: E/AndroidRuntime(1432): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您的NumberPickers对象为空。
如果在renameDialog
的布局中声明它们,则尝试在promptsView
布局中按ID查找视图,如下所示:
whatin = (NumberPicker)promptsView.findViewById(R.id.inpicker);
whatout = (NumberPicker)promptsView.findViewById(R.id.outpicker);