Not converting INTEGER to String

Asked

Viewed 41 times

1

Good Afternoon, my application has the function of monitoring virtually a football game, and it contains a button that saves the name of the team and the number of goals that the team made... Only when I click the save button it says that the application has stopped, I took a look at Logcat and apparently shows that it is not converting my INTEGER to String, I did the process 'Integer.parseInt' only that it is giving error.

class:

package com.allsport.miyonic.allsport;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import Base.*;

public class SimplesHome extends AppCompatActivity {

    private ImageButton imgButton_play, imgButton_pause, imgButton_1, imgButton_2;
    private Button saver, results;
    public TextView valorOne;
    public TextView valorDouble;
    public int contador = 0;
    public int contador1 = 0;
    private Chronometer reloginho;
    private EditText casa, fora;
    long tempoPausado = 0;

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

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        imgButton_1 = (ImageButton) findViewById(R.id.imgButton_1);
        imgButton_2 = (ImageButton) findViewById(R.id.imgButton_2);
        imgButton_play = (ImageButton) findViewById(R.id.imgButton_play);
        imgButton_pause = (ImageButton) findViewById(R.id.imgButton_pause);
        reloginho = (Chronometer) findViewById(R.id.chronometer);
        valorOne = (TextView) findViewById(R.id.txt_valor1);
        valorDouble = (TextView) findViewById(R.id.txt_valor2);
        casa = (EditText) findViewById(R.id.lbl_time1);
        fora = (EditText) findViewById(R.id.lbl_time2);
        saver = (Button) findViewById(R.id.save);
        results = (Button) findViewById(R.id.btnr);


        imgButton_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contador++;
                valorOne.setText(" " + contador);
            }
        });

        imgButton_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contador1++;
                valorDouble.setText(" " + contador1);
            }
        });

        imgButton_play.setEnabled(true);
        imgButton_pause.setEnabled(false);
        imgButton_1.setEnabled(false);
        imgButton_2.setEnabled(false);


        imgButton_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgButton_play.setEnabled(false);
                imgButton_pause.setEnabled(true);
                imgButton_1.setEnabled(true);
                imgButton_2.setEnabled(true);

                reloginho.setBase(SystemClock.elapsedRealtime());
                reloginho.start();
                reloginho.setBase(SystemClock.elapsedRealtime() + tempoPausado);
            }
        });

        imgButton_pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgButton_play.setEnabled(true);
                imgButton_pause.setEnabled(false);
                imgButton_1.setEnabled(false);
                imgButton_2.setEnabled(false);

                tempoPausado = reloginho.getBase();
                SystemClock.elapsedRealtime();
                reloginho.stop();
            }
        });

    }


    public void SalvarPartida (View view){
        Esporte inserir = new Esporte();

        inserir.setNomeTimeUm(casa.getText().toString());
        inserir.setNomeTimeDois(fora.getText().toString());
        inserir.setValorUm(Integer.parseInt(valorOne.getText().toString()));
        inserir.setValorDois(Integer.parseInt(valorDouble.getText().toString()));

        DbHelper dbhelp = new DbHelper(this);
        dbhelp.insertResultado(inserir);


        finish();
    }

    public void SalvarVermelho (View view){
        Intent vermelho = new Intent(this, CartaoVermelho.class);
        startActivity(vermelho);
    }

    public void SalvarAmarelo (View view){
        Intent amarelo = new Intent(this, CartaoAmarelo.class);
        startActivity(amarelo);
    }


    public void ress (View view){
        Intent result = new Intent(this, ResultSimples.class);
        startActivity(result);
    }
}

valueOne and valueDouble it shows as number....

Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.allsport.miyonic.allsport, PID: 3594
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                      at android.view.View.performClick(View.java:4780)
                      at android.view.View$PerformClick.run(View.java:19866)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                      at android.view.View.performClick(View.java:4780) 
                      at android.view.View$PerformClick.run(View.java:19866) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                   Caused by: java.lang.NumberFormatException: Invalid int: " 2"
                      at java.lang.Integer.invalidInt(Integer.java:138)
                      at java.lang.Integer.parse(Integer.java:410)
                      at java.lang.Integer.parseInt(Integer.java:367)
                      at java.lang.Integer.parseInt(Integer.java:334)
                      at com.allsport.miyonic.allsport.SimplesHome.SalvarPartida(SimplesHome.java:108)
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                      at android.view.View.performClick(View.java:4780) 
                      at android.view.View$PerformClick.run(View.java:19866) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

Thank you...

1 answer

7


See the cause of the error:

Caused by: java.lang.NumberFormatException: Invalid int: " 2"

There is a space before the number.

Before converting use the method Trim() of the string class to delete any spaces.

inserir.setValorUm(Integer.parseInt(valorOne.getText().toString().trim()));
  • I managed to arrange here... thank you again.

Browser other questions tagged

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