I am trying to turn 2 number pickers to a string but it never seems to work.

I just keep getting an NPE and cannot seem to figure out the cause.

See code and Logcat below,

Any help is appreciated.

The below is within 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)
有帮助吗?

解决方案

Your NumberPickers objects are null.
If they are declared in the layout of renameDialog, then try to find the view by id in the promptsView layout, like this:

whatin = (NumberPicker)promptsView.findViewById(R.id.inpicker);
whatout = (NumberPicker)promptsView.findViewById(R.id.outpicker);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top