Textview when clicking , increase certain value that is in Edit text

Asked

Viewed 218 times

0

I’m making a test application that has 1 Edit and a textview , Edit text will have a value within the initial 0 , when you click the textview , I want it to increase to 10.00 , and if you click again increase to 20 , and so on

        teste = findViewById(R.id.add10);
        teste2 = findViewById(R.id.editText);

        teste.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int a=Integer.parseInt(teste2.getText().toString());
                int b=a+10;
                teste2.setText(new Integer(b).toString());
            }
        });
    }
}

Segue logcat

01-22 10:23:48.987 8645-8645/com.example.gabriel.teste E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
01-22 10:23:49.100 8645-8645/com.example.gabriel.teste E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.example.gabriel.teste, PID: 8645
 java.lang.NumberFormatException: For input string: "Name"
     at java.lang.Integer.parseInt(Integer.java:521)
     at java.lang.Integer.parseInt(Integer.java:556)
     at com.example.gabriel.teste.MainActivity$1.onClick(MainActivity.java:25)
     at android.view.View.performClick(View.java:6261)
     at android.widget.TextView.performClick(TextView.java:11159)
     at android.view.View$PerformClick.run(View.java:23748)
     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:6776)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
  • 1

    It did not work out is a little vague. Did not run ? Gave error ? Did not do anything ? Did not add the amount that was supposed ? Try to be a little more specific in the problem you have.

  • 1

    Be more specific about the error that occurred, and pass on more information to help us help you.

  • Guys , I put the logcat in the question

  • What value are you passing in textview and edittext?

  • @Antonios.Junior 10

  • 1

    This error happens when you pass some null value or parse cannot convert. It seems to me that you have some string "Name" in your app and he is trying to convert to Integer.

  • @Antonios.Junior thank you , the error was because Edit had the name value , I managed to solve

  • good! I’ll just add as an answer to serve other people who have the same problem.

Show 3 more comments

3 answers

2

Friend, First of all set in your textview that it should accept only numbers:

android:inputType="number"

Then, just put a setOnClickListener to increase the value:

tvAdd10 = (TextView) findViewById(R.id.add10);
tvValorAtual = (EditText) findViewById(R.id.editText);

tvAdd10.setOnClickListener(new View.OnClickListener(){
  @Override
  public void onClick(View view){
    int valorAtual = Integer.parseInt(tvValorAtual.getText().toString());
    int novoValor = valorAtual + 10;
    tvValorAtual.setText(String.valueOf(novoValor));
  }
});

1

    teste = findViewById(R.id.add10);
        teste2 = findViewById(R.id.editText);

        teste.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int a=Integer.parseInt(teste2.getText().toString());
                int b=a+10;
                teste2.setText(String.valueOf(b));
            }
        });
  • Don’t forget to put 0 if edittex is empty

1


This error happens when you pass some "null" value or parse cannot convert. It seems to me that you have some string "Name" in your app and he is trying to convert to Integer.

A good practice when casting is always to check if the value can return null or in some format that the type cannot convert.

Browser other questions tagged

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