1

我正在尝试将 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)
4

1 回答 1

2

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

whatin = (NumberPicker)promptsView.findViewById(R.id.inpicker);
whatout = (NumberPicker)promptsView.findViewById(R.id.outpicker);
于 2013-01-04T12:07:34.797 回答