I get the following error in android studio

-2

Error message in Logcat

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

The following code is summarized, from this point on it gives the same error

package com.example.admin.tallerandroid.fede.tallerandroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TallerAndroid extends AppCompatActivity {
    Button btn;
    TextView txt;
    EditText v1;
    TextView res;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_taller_android);

        this.btn=(Button)this.findViewById(R.id.btn1);
        this.txt=(TextView)this.findViewById(R.id.textView) ;
        this.v1=(EditText)this.findViewById(R.id.val1) ;

        final String vv1 = v1.getText().toString();
        final int a = Integer.parseInt(vv1);
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TallerAndroid">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="51dp"
        android:textSize="40px"
        android:text="@string/sumar" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/textView"
        android:layout_centerVertical="true"
        android:text="@string/btn1" />

    <EditText
        android:id="@+id/val1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="109dp"
        android:ems="10"
        android:inputType="number" />

    <EditText
        android:id="@+id/val2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="161dp"
        android:ems="10"
        android:inputType="number" />

    <TextView
        android:id="@+id/resultado"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="152dp"
        android:layout_marginStart="139dp"
        android:text="TextView" />

</RelativeLayout>
    
asked by Federico M 03.08.2018 в 17:05
source

2 answers

0

The problem is that you are wanting to parse an empty string as an integer. The sequence in your activity is: - the activity is created - the contents of v1 are saved in vv1. Here v1 is empty because the activity is just created. - Attempt to convert the content of vv1 (still empty for the above) to integer. That throws the exception.

Notice that the log says Invalid int: "" that double quote indicates that you are trying to convert an empty string.

You should make sure vv1 is not empty and only contains numbers before pairing it

    
answered by 03.08.2018 / 17:14
source
-1

the error is found in the following lines: final String vv1 = v1.getText().toString(); final int a = Integer.parseInt(vv1);

The problem is that you are trying to convert an empty String to an entire String, which is incorrect.

in the LogCat tells you the error: java.lang.NumberFormatException: Invalid int: ""

The double quotes means empty, what you can do to solve the error is the following: int a ; if (vv1.isEmpty()) { a = 0; }else{ a = Integer.parseInt(vv1); }

This way you avoid the error when initializing your variable a.

    
answered by 03.08.2018 в 17:28