gpt4 book ai didi

java - Android NumberPicker 转字符串

转载 作者:行者123 更新时间:2023-11-29 21:52:03 24 4
gpt4 key购买 nike

我正在尝试将 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();
}
});

日志猫

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)

最佳答案

您的 NumberPickers 对象为空。
如果它们在 renameDialog 布局中声明,则尝试在 promptsView 布局中通过 id 查找 View ,如下所示:

whatin = (NumberPicker)promptsView.findViewById(R.id.inpicker);
whatout = (NumberPicker)promptsView.findViewById(R.id.outpicker);

关于java - Android NumberPicker 转字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14156814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com