Android: Unfortunately, Sample has stopped

Asked

Viewed 86 times

0

I’m creating an application but in case it gives problem when running 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 geoi and Mati int which in the case is equal to 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);   
    }

}
  • 3

    Gustavo, you need to include the stacktrace error that appears in your log IDE. Without this you can not guess which error occurred in your code, because at first there is no.

  • @Gustavo Dibai tries to make his class inherit from Activity.. public class Mainactivity extends Activity.. another say and check the names in findViewById to see if it is not wrong id

  • Actionbaractivity has the same methods of Activity but some complementary.

  • Wakim I’ll try that vlw

  • @Wakim could you help me do the stacktrace ? Inside my code ?

  • Look at the Logcat from the IDE, you need to be plugged into the USB and running the ADB.

  • Androidruntime at java.lang.Integer.parseint(Integer.java:366)

  • Copy the stacktrace whole, it has dozens of lines, in his question for better visualization.

  • @Gustavodibai, the stacktrace is already being shown, but need to check in your IDE, which one you’re wearing?

  • @Wakim Aee I think I found 09-29 20:07:22.892: E/androidruntime(1437): FATAL EXCEPTION: main 09-29 20:07:22.892: E/Androidruntime(1437): java.lang.Numberformatexception: Invalid int: "1.5" 09-29 20:07:22.892: E/Androidruntime(1437): at java.lang.Integer.invalidInt(Integer.java:138) 09-29 20:07:22.892: E/Androidruntime(1437): at java.lang.Integer.parse(Integer.java:375) 09-29 20:07:22.892: E/Androidruntime(1437): at java.lang.Integer.parseint(Integer.java:366) 09-29 20:07:22.892: E/Androidruntime(1437): at java.lang.Integer.parseint(Integer.java:332)

  • @Wakim think I’ve got the bug, I’m putting decimals, and it’s only integer numbers over and now what I’ve done to run the decimals too ?

Show 6 more comments

1 answer

0


By Stacktrace posted, the problem is that it is converting a String in format float for a int.

I don’t know if your intention is to treat the field value as integer or decimal.

To treat as integer number you must do:

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

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

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

float counterAsFloat = Float.parseFloat(counter);

And then change the type of the other variables int used in the calculation for 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 it is converting a variable of type float for its representation in String.

  • Valeu helped a lot :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.