java.lang.Numberformatexception: Invalid int: ""

Asked

Viewed 423 times

-2

I am beginner and every time I try to make my application run from error, please help me I have to finish this as soon as possible

package com.example.chuveiroappuva;

import com.example.chuveiroappuva.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;


public class ChuveiroAppUVA extends Activity {

    EditText editText1;
    EditText editText2;
    EditText editText3;
    EditText editText4;
    CheckBox checkBox1;
    RadioButton radio0;
    RadioButton radio1;
    RadioButton radio2;
    RadioButton radio3;
    RadioButton radio4;
    Button button1;
    Button button2;

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

        editText1 = (EditText) findViewById(R.id.editText1);
        editText2 = (EditText) findViewById(R.id.editText2);
        editText3 = (EditText) findViewById(R.id.editText3);
        editText4 = (EditText) findViewById(R.id.editText4);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        radio0 = (RadioButton) findViewById(R.id.radio0);
        radio1 = (RadioButton) findViewById(R.id.radio1);
        radio2 = (RadioButton) findViewById(R.id.radio2);
        radio3 = (RadioButton) findViewById(R.id.radio3);
        radio4 = (RadioButton) findViewById(R.id.radio4);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);

        button1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0){
            }
        });

        int numero =0, quantidade =0, banhos = 0;
        numero = Integer.parseInt(editText1.getText().toString());
        quantidade = Integer.parseInt(editText2.getText().toString());
        banhos = numero * quantidade;
        editText3.setText(String.valueOf(banhos));

        button2.setOnClickListener(new OnClickListener(){
            private boolean eletrico;

            @Override
            public void onClick(View arg0){

                @SuppressWarnings("unused")
                int numero =0, quantidade =0, banhos = 0;
                double cm;
                numero = Integer.parseInt(editText1.getText().toString());
                quantidade = Integer.parseInt(editText2.getText().toString());
                //Verificação de Chuveiro
                if(checkBox1.isChecked()){
                    setEletrico(true);
                }
                else{
                    setEletrico(false);
                }
                //Processamento
                quantidade = numero * banhos;
                editText3.setText(String.valueOf(banhos));
                cm= 14.88 * numero * banhos;


                if(radio0.isChecked()){
                    cm = 14.88 * numero * banhos;
                }
                else if(radio1.isChecked()){
                    cm = 20.93 * numero * banhos;
                }
                else if(radio2.isChecked()){
                    cm = 25.58 * numero * banhos;
                }
                else if(radio3.isChecked()){
                    cm = 27.90 * numero * banhos;
                }
                else if(radio4.isChecked()){
                    cm = 34.88 * numero * banhos;
                }
                editText4.setText(String.valueOf(cm));

            }
            @SuppressWarnings("unused")
            public boolean isEletrico() {
                return eletrico;
            }

            public void setEletrico(boolean eletrico) {
                this.eletrico = eletrico;
            }




            });
    }
}

Error presented:

06-11 22:47:18.216: E/AndroidRuntime(3738): FATAL EXCEPTION: main
06-11 22:47:18.216: E/AndroidRuntime(3738): Process: com.example.chuveiroappuva, PID: 3738
06-11 22:47:18.216: E/AndroidRuntime(3738): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chuveiroappuva/com.example.chuveiroappuva.ChuveiroAppUVA}: java.lang.NumberFormatException: Invalid int: ""
06-11 22:47:18.216: E/AndroidRuntime(3738):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at android.os.Looper.loop(Looper.java:136)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at android.app.ActivityThread.main(ActivityThread.java:5001)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at java.lang.reflect.Method.invoke(Method.java:515)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at dalvik.system.NativeStart.main(Native Method)
06-11 22:47:18.216: E/AndroidRuntime(3738): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 22:47:18.216: E/AndroidRuntime(3738):     at java.lang.Integer.invalidInt(Integer.java:137)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at java.lang.Integer.parseInt(Integer.java:358)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at java.lang.Integer.parseInt(Integer.java:331)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at com.example.chuveiroappuva.ChuveiroAppUVA.onCreate(ChuveiroAppUVA.java:54)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at android.app.Activity.performCreate(Activity.java:5231)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-11 22:47:18.216: E/AndroidRuntime(3738):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-11 22:47:18.216: E/AndroidRuntime(3738):     ... 11 more

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="ChuveiroAppUVA Por UVA" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Numero de Pessoas"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:inputType="number" >
    android:text="1"/>


    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:text="Quantidade de Banhos Pessoa"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:inputType="number" />
    android:text="1"/>

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:text="Calcular" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:text="Banhos Tomados Diariamente"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView3"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:inputType="number" />
    android:text="1"/>

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText3"
    android:layout_centerHorizontal="true"
    android:text="Eletrico?" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView4"
    android:layout_alignLeft="@+id/button1"
    android:layout_marginBottom="16dp"
    android:text="Calcular" />

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/checkBox1" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="3200W" />
</RadioGroup>

<RadioButton
    android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/radioGroup1"
    android:layout_toRightOf="@+id/radioGroup1"
    android:text="4500W" />

<RadioButton
    android:id="@+id/radio2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/radio1"
    android:layout_alignBottom="@+id/radio1"
    android:layout_alignRight="@+id/textView4"
    android:text="5500W" />

<RadioButton
    android:id="@+id/radio3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/radioGroup1"
    android:text="6000W" />

<RadioButton
    android:id="@+id/radio4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView5"
    android:layout_alignParentLeft="true"
    android:text="7500W" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/editText4"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="27dp"
    android:text="Custo Mensal"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button2"
    android:layout_alignLeft="@+id/editText3"
    android:ems="10"
    android:inputType="numberDecimal" />
    android:text="1"/>

  • from what I can see from your code, it’s probably because Voce is trying to convert an integer value from an empty edittext, trying to put a default value on it in your Activity.xml declaration, type: <EditText ... android:inputType="number" android:ems="10" android:text="1" /> if it works let us know, if it doesn’t let us know.

  • I just made that continued the same mistake

  • put the . xml file of your layout that I test here and put the result

  • Or then, first takes the edittext value in a string String txt1 = editText1.getText().toString(); and then convert that value to int numero = Integer.parseInt(text1)); and marks a breakpoint and debug to see the values being passed in your code.

  • I’m not managing to make this change has how to help me??

1 answer

0


You’re doing it wrong, here’s your . xml fixed:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="ChuveiroAppUVA Por UVA" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Numero de Pessoas"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:inputType="number"
    android:text="1">
    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:text="Quantidade de Banhos Pessoa"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:inputType="number"
    android:text="1"/>

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:text="Calcular" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:text="Banhos Tomados Diariamente"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView3"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:inputType="number" 
    android:text="1"/>

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText3"
    android:layout_centerHorizontal="true"
    android:text="Eletrico?" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView4"
    android:layout_alignLeft="@+id/button1"
    android:layout_marginBottom="16dp"
    android:text="Calcular" />

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/checkBox1" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="3200W" />
</RadioGroup>

<RadioButton
    android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/radioGroup1"
    android:layout_toRightOf="@+id/radioGroup1"
    android:text="4500W" />

<RadioButton
    android:id="@+id/radio2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/radio1"
    android:layout_alignBottom="@+id/radio1"
    android:layout_alignRight="@+id/textView4"
    android:text="5500W" />

<RadioButton
    android:id="@+id/radio3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/radioGroup1"
    android:text="6000W" />

<RadioButton
    android:id="@+id/radio4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView5"
    android:layout_alignParentLeft="true"
    android:text="7500W" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/editText4"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="27dp"
    android:text="Custo Mensal"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button2"
    android:layout_alignLeft="@+id/editText3"
    android:ems="10"
    android:inputType="numberDecimal" 
    android:text="1"/>
</RelativeLayout>

Here’s a screenshot of the app running on my emulator, a 4.7 inc with api 22.

hope it helps!

  • the screen turned all gray after I changed xml and my layout disappeared

  • revert your code and copy only the components of the first textview until <\RelativeLayout> because I had to use this one to test here

  • Armando he worked only that he does not make any kind of calculation, what can be??

  • well, then you need to review your code there, do what I told you, debug your code

  • I don’t know how this debug works, but I did and it didn’t help.

  • now this doing was a fool of mine, but I’m not able to use the radiobutton right they do not deselect when I click on another

Show 1 more comment

Browser other questions tagged

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