javaandroid

Android App custom Number Picker


So I'm currently making an app that requires number pickers. I have create the number picker using a -button textview +button layout. tThe picker works fine apart from when you delete the number and try and press any of the buttons, at this point the application crashes.

public class MainActivity extends Activity implements OnClickListener {
Button btnUp, btnDown;
EditText editTextSonyResult;

int nStart = (-100);
int nEnd = 250;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnUp = (Button) findViewById(R.id.plusButton1);
    btnDown = (Button) findViewById(R.id.minusButton1);


    editTextSonyResult = (EditText) findViewById(R.id.numberView1);

    editTextSonyResult.setText("0");

    btnUp.setOnClickListener(this);
    btnDown.setOnClickListener(this);
}

public void onClick(View v) {
    String getString = String.valueOf(editTextSonyResult.getText());
    int current = Integer.parseInt(getString);


    if (v == btnUp) {
        if (current < nEnd) {

            current++;
            editTextSonyResult.setText(String.valueOf(current));
        }

    }
    if (v == btnDown) {
        if (current > nStart) {

            current--;
            editTextSonyResult.setText(String.valueOf(current));

        }
    }
}

Any help would be appreciated. Cheers

here is the logcat at the point of the error 04-27 19:58:10.042 3016-3016/com.example.liam.sonyreportingapp D/AndroidRuntime﹕ Shutting down VM 04-27 19:58:10.042 3016-3016/com.example.liam.sonyreportingapp E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.liam.sonyreportingapp, PID: 3016 java.lang.NumberFormatException: Invalid int: "" at java.lang.Integer.invalidInt(Integer.java:138) at java.lang.Integer.parseInt(Integer.java:358) at java.lang.Integer.parseInt(Integer.java:334) at com.example.liam.sonyreportingapp.MainActivity.onClick(MainActivity.java:42) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)


Solution

  • I made a custom NumberPicker class that you can simply add to your layout xml file.

    Custom NumberPicker class on github

    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.NumberPicker;
    
    /**
     * Created by Marlon on 26.03.2015.
     */
    public class MyNumberPicker extends NumberPicker {
        public MyNumberPicker(Context context) {
            super(context);
        }
    
        public MyNumberPicker(Context context, AttributeSet attrs) {
            super(context, attrs);
            processAttributes(attrs);
        }
    
        private void processAttributes(AttributeSet attrs) {
            this.setMinValue(attrs.getAttributeIntValue(null, "min", 0));
            this.setMaxValue(attrs.getAttributeIntValue(null, "max", 100));
            this.setValue(attrs.getAttributeIntValue(null, "initial", (this.getMaxValue() - this.getMinValue()) / 2)));
        }
    
        public MyNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            processAttributes(attrs);
        }
    }