Android - EditText does not return typed value

0

I'm trying to retrieve a value to generate a random password, however the field   "size" is not returning what you typed.

The error occurs in the following block:

int tamanho = Integer.parseInt(findViewById(R.id.tamanho).toString());

Follow the codes:

.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout
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=".MainActivity">

<Button
    android:id="@+id/gerar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Gerar"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.971"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.107" />

<TextView
    android:id="@+id/senhaGerada"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Senha Gerada"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.247" />

<TextView
    android:id="@+id/labelTamanho"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Tamanho:"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.66"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.041" />

<TextView
    android:id="@+id/labelSenhaGerada"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Senha Gerada:"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.028"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.206" />

<EditText
    android:id="@+id/tamanho"
    android:layout_width="88dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:gravity="end"
    android:hint="Tamanho"
    android:inputType="number"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.968"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.015" />

.java

package granbery.edu.br.lucaslinhares.password;

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

import java.util.Random;

public class MainActivity extends AppCompatActivity {

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

        Button gerar = findViewById(R.id.gerar);
        final TextView senhaGerada = findViewById(R.id.senhaGerada);

        gerar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int tamanho = Integer.parseInt(findViewById(R.id.tamanho).toString());
                String novaSenha = "";
                Random random = new Random();
                Character caracter;
                for (int i = 0; i < tamanho; i++) {
                    do {
                        caracter = (char) random.nextInt(255);
                    } while (!Character.isAlphabetic(caracter));
                    novaSenha += caracter;
                }
                senhaGerada.setText(novaSenha);
            }
        });

    }
}

Exception

08-26 20:38:03.173 13405-13405/granbery.edu.br.lucaslinhares.password E/AndroidRuntime: FATAL EXCEPTION: main
Process: granbery.edu.br.lucaslinhares.password, PID: 13405
java.lang.NumberFormatException: For input string: "android.support.v7.widget.AppCompatEditText{7e1d49e VFED..CL. .F...... 767,44-1031,180 #7f070086 app:id/tamanho}"
    at java.lang.Integer.parseInt(Integer.java:521)
    at java.lang.Integer.parseInt(Integer.java:556)
    at granbery.edu.br.lucaslinhares.password.MainActivity$1.onClick(MainActivity.java:24)
    at android.view.View.performClick(View.java:5612)
    at android.view.View$PerformClick.run(View.java:22288)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6123)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

If anyone knows, I'll be very grateful!

    
asked by anonymous 27.08.2018 / 01:56

2 answers

1

Do this:

EditText tamanho = findViewById(R.id.tamanho);

int tam = Integer.parseInt(tamanho.getText().toString());

This should resolve

The error it seems, you are not assigning a type to the referenced view, in addition to just toString() does not collect the text of a EditText , needs getText() first.

    
27.08.2018 / 02:37
-1

Try doing the following:

int tamanho = Integer.parseInt((EditText) findViewById(R.id.tamanho).getText().toString());

    
31.08.2018 / 23:39