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

0

I'm doing a test application that has 1 edit and a textview, the edit text will have a value inside initial 0, when I click on the textview, I want it to increase to 10.00, and if I 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());
            }
        });
    }
}

Follow 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)
    
asked by anonymous 22.01.2018 / 03:03

3 answers

1

This error happens when you pass some value "null" or that parse can not convert. It looks like you have some "Name" string in your app and it's trying to convert to Integer.

A good practice when casting is to always check whether the value can return null or in any format the type can not convert.

    
22.01.2018 / 15:53
2

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

android:inputType="number"

Then, just put a setOnClickListener to increment 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));
  }
});
    
22.01.2018 / 15:55
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));
            }
        });
    
22.01.2018 / 15:35