开发者

Problem while implementing EditText field dynamically

开发者 https://www.devze.com 2023-03-31 04:44 出处:网络
I am entering floating numbers into my EditText fields and want to calculate the result based on some logic as below and display the result in a TextView.I wanted to know what is the problem with this

I am entering floating numbers into my EditText fields and want to calculate the result based on some logic as below and display the result in a TextView.I wanted to know what is the problem with this method since I am getting error with this particular method.I know its a simple thing but then I am an Android beginner.

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.bmi);
            bmi1=(EditText) findViewById(R.id.editText1);
            bmi2=(EditText) findViewById(R.id.editText2);
          bmi3=(TextView) findViewById(R.id.bmiText);


    bmi2.addTextChangedListener(new TextWatcher(){
        @Override
        public void afterTextChanged(Editable ab) {
            // TODO Auto-generated method stub

        }
          public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){
            calRes();
        }
    });




    ImageButton ImBtn=(ImageButton)findViewById(R.id.imageButton1);
    ImBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent newIntent=new Intent(Activity1.this,DragNDropListActivity.class);
            startActivity(newIntent);
            finish();
        }
    });

}

    public void calRes() {
        String a; 
         String b;  
        float c;
         a =  bmi1.getEditableText().toString();   //gets you the contents of edit text
         b =  bmi2.getEditableText().toString(); 
         c=  Float.parseFloat(a)/(Float.parseFloat(b)*Float.parseFloat(b));    
         bmi3.setText(String.valueOf(c));
    }

}

Error:

     08-29 12:05:03.658: ERROR/AndroidRuntime(299): FATAL EXCEPTION: main
     08-29 12:05:03.658: ERROR/AndroidRuntime(299): java.lang.NumberFormatException:         lengte in metre
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:130)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:319)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at java.lang.Float.parseFloat(Float.java:291)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at  com.android.mds.health.nursing.formula.Activity1.calRes(Activity1.java:87)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at com.android.mds.health.nursing.formula.Activity1$1.onTextChanged(Activity1.java:45)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.widget.TextView.handleTextChanged(TextView.java:6172)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:400)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:218)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:28)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.t开发者_如何学编程ext.method.BaseKeyListener.backspace(BaseKeyListener.java:60)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:123)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:327)
     08-29 12:05:03.658: ERROR/AndroidRuntime(299):     at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)


Instead of this

bmi3.setText(String.valueOf(c));

do this

bmi3.setText(""+c);

EDIT:
Use valueOf instead of parseFloat

  c=  Float.valueOf(a)/(Float.valueOf(b)*Float.valueOf(b)); 

Edit 2 : Calculate the BMI when all values are entered into the edittexts. Have a button and call calRes in onClick of the button. You are getting the exception because calRes gets called when you are still entering the first field and rest fields are empty. Empty fields are causing the crash coz there is no value in them to be converted.


ok, after watching your code its seem to, you have a problem in parsing string to float,

If you have a not problem of values of string a and b, then it should work fine.

look, I have a tried this its work perfect,

       String a,b;
       Float c;
      tv =(TextView)findViewById(R.id.tv_test);
        a =  "100";   //gets you the contents of EditText
        b =  "10"; 
        c=  Float.parseFloat(a)/(Float.parseFloat(b)*Float.parseFloat(b));    
      tv.setText(String.valueOf(c));


Edit: Also take care of your editText input its have only numeric values in string
format, not any characters or special symbols.


Maybe instead of this,

    a =  bmi1.getText().toString();   //gets you the contents of EditText
    b =  bmi2.getText().toString(); 

you should try this,

    a =  bmi1.getEditableText().toString();   //gets you the contents of EditText
    b =  bmi2.getEditableText().toString(); 


If any alphabet or special characters in your EditText (bmi1 & bmi2 , a ,b) then this error must be coming so please check your value of EditText using System.out.println..becuase float Variablt must be numeric and dot (.)


from looking at your initial error it seems as though you are trying to parse a float from the string 'lengte in metre'. Is this the text of a label in your layout? It sure is not a parseable number:) Maybe if you are still having problems you can post the xml layout file.

Anthony

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号