Android: Unfortunately, Sample has stopped

0

I am creating an application but in case it is giving a problem when running up to a certain point, I want to transform geoi = 0; and mati = 0; , and when I add any number in edittext it will put that number inside the int geoi and mati that in the case equals 0. What is the error in my code? Why are you giving Unfortunately?

package com.gustavo.sample;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    CheckBox g;
    CheckBox m;
    Button send;
    TextView say;
    EditText num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bacon();

        send.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String counter = num.getText().toString();

                int counterAsInt = Integer.parseInt(counter);

                int geoi = 0;
                int mati = 0;           



                  if(g.isChecked()){
                      geoi += counterAsInt;
                      say.setText("Geo" + geoi);
                  }
                  else if(m.isChecked()){  
                    mati += counterAsInt;
                    say.setText("Math" + mati);
                  }

            }
        });
    }
    public void bacon() {
        g = (CheckBox)findViewById(R.id.checkBox1);
        m = (CheckBox)findViewById(R.id.checkBox2);
        send = (Button)findViewById(R.id.button1);
        say = (TextView)findViewById(R.id.textView1);
        num = (EditText)findViewById(R.id.editText1);   
    }

}
    
asked by anonymous 29.09.2014 / 00:25

1 answer

0

By Stacktrace posted, the problem is that you are converting String to float to int .

I do not know if your intent is to treat the value of the field as an integer or as a decimal number.

To treat as integer you should do:

int counterAsInt = (int) Float.parseFloat(counter);

With this, the value String of counter will be converted to decimal number ( float ) and then converted to integer through a casting , with this it will discard the part decimal.

If you want to treat as a decimal number you should ignore casting and use a variable of type float , like this:

float counterAsFloat = Float.parseFloat(counter);

And then change the type of other variables int used in the calculation to float also:

float geof = 0f;
float matf = 0f;           

if(g.isChecked()){
    geof += counterAsFloat;
    say.setText("Geo" + Float.toString(geof));
} else if(m.isChecked()){  
    matf += counterAsFloat;
    say.setText("Math" + Float.toString(matf));
}

The use of Float.toString is optional, but makes it clear that you are converting a variable of type float to its representation in String .

    
29.09.2014 / 22:16