EditText更新SeekBar崩溃

时间:2014-06-10 16:01:03

标签: android view android-edittext integer seekbar

我有两个问题。 1:所以我有这个edittext,每次删除数字时,指针都会转到第一个数字。我怎么改变它? 2: 当我从Edittext中删除所有数字时,应用程序崩溃了。 我看到了LogCat并添加了例外..但我不知道如何防止它崩溃。 提前谢谢。

Activty

public SeekBar FlatPrice = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.publishflat);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    final EditText flatPriceET = (EditText) findViewById(R.id.editText1);
    flatPriceET.addTextChangedListener(new TextWatcher() {
        @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {
        Integer i = Integer.parseInt(s.toString());
if (i!= null){
        if (i >= 0 && i <= 10070) {
            try {
                FlatPrice.setProgress(i);
            } catch (NumberFormatException e) {
                Toast.makeText(getApplicationContext(),
                        "write your number", Toast.LENGTH_LONG).show();
            }

        }
    }
    }
});

// seekBar functionality
FlatPrice = (SeekBar) findViewById(R.id.seekBar1);
FlatPrice.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub
            flatPriceET.setText("" + String.valueOf(progress));

        }
    });
    // end of seekBar Listener
}

logcat的:

    06-10 18:59:38.986: E/AndroidRuntime(13390): FATAL EXCEPTION: main
06-10 18:59:38.986: E/AndroidRuntime(13390): java.lang.NumberFormatException: Invalid int: ""
06-10 18:59:38.986: E/AndroidRuntime(13390):    at java.lang.Integer.invalidInt(Integer.java:138)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at java.lang.Integer.parseInt(Integer.java:359)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at java.lang.Integer.parseInt(Integer.java:332)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at nir.rauch.flantirflats.PublishYourFLat$1.afterTextChanged(PublishYourFLat.java:40)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.widget.TextView.sendAfterTextChanged(TextView.java:8503)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10633)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:212)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:30)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:94)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:49)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:155)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:138)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.widget.TextView.doKeyDown(TextView.java:6470)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.widget.TextView.onKeyDown(TextView.java:6267)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.KeyEvent.dispatch(KeyEvent.java:2891)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.View.dispatchKeyEvent(View.java:7677)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1485)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1485)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1485)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1485)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2293)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1602)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.app.Activity.dispatchKeyEvent(Activity.java:2451)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2211)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4562)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4538)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4143)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4193)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4162)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4247)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4170)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4304)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4143)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4193)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4162)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4170)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4143)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6288)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6226)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6197)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6162)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3744)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.os.Looper.loop(Looper.java:137)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at android.app.ActivityThread.main(ActivityThread.java:5419)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at java.lang.reflect.Method.invokeNative(Native Method)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at java.lang.reflect.Method.invoke(Method.java:525)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
06-10 18:59:38.986: E/AndroidRuntime(13390):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

试试这个..

虽然在最后afterTextChanged删除数字会收到空的“”,以便NumberFormatException尝试检查条件如下

@Override
public void afterTextChanged(Editable s) {

if(!s.toString().trim().equals("")){
    Integer i = Integer.parseInt(s.toString());
if (i!= null){
    if (i >= 0 && i <= 10070) {
        try {
            FlatPrice.setProgress(i);
        } catch (NumberFormatException e) {
            Toast.makeText(getApplicationContext(),
                    "write your number", Toast.LENGTH_LONG).show();
        }

    }
}
}else{
     FlatPrice.setProgress(0);
}
}