onClick on Android

Asked

Viewed 317 times

2

I made a EditText with the attribute text = 100, but when I use my function onClick(), which aims to make the following calculation text - 50, when I click the button and runs the routine my application closes.

Follows the code:

public class Principal extends AppCompatActivity {
    Button botao1;
    TextView life;

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

        botao1 = (Button) findViewById(R.id.btn1);
        life = (TextView) findViewById(R.id.hp);

        botao1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                int ataque = Integer.parseInt(life.getText().toString());
                int ataca = 50;
                life.setText(ataque - 50);
            }
        }
    }
}

Follow the Logcat:

 01-18 21:58:14.640 16746-16746/? I/art: Late-enabling -Xcheck:jni
    01-18 21:58:15.647 16746-16798/poke.pokeclub D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
    01-18 21:58:15.705 16746-16746/poke.pokeclub D/Atlas: Validating map...
    01-18 21:58:15.750 16746-16756/poke.pokeclub W/art: Suspending all threads took: 6.579ms
    01-18 21:58:15.861 16746-16798/poke.pokeclub I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build:  ()
    01-18 21:58:15.861 16746-16798/poke.pokeclub I/Adreno-EGL: OpenGL ES Shader Compiler Version: E031.25.03.04
    01-18 21:58:15.861 16746-16798/poke.pokeclub I/Adreno-EGL: Build Date: 04/06/15 Mon
    01-18 21:58:15.861 16746-16798/poke.pokeclub I/Adreno-EGL: Local Branch: 
    01-18 21:58:15.861 16746-16798/poke.pokeclub I/Adreno-EGL: Remote Branch: 
    01-18 21:58:15.861 16746-16798/poke.pokeclub I/Adreno-EGL: Local Patches: 
    01-18 21:58:15.861 16746-16798/poke.pokeclub I/Adreno-EGL: Reconstruct Branch: 
    01-18 21:58:15.865 16746-16798/poke.pokeclub I/OpenGLRenderer: Initialized EGL, version 1.4
    01-18 21:58:15.976 16746-16798/poke.pokeclub D/OpenGLRenderer: Enabling debug mode 0
    01-18 21:58:16.782 16746-16756/poke.pokeclub W/art: Suspending all threads took: 6.420ms
    01-18 21:58:16.998 16746-16746/poke.pokeclub I/Choreographer: Skipped 57 frames!  The application may be doing too much work on its main thread.
    01-18 21:58:19.916 16746-16746/poke.pokeclub I/Choreographer: Skipped 33 frames!  The application may be doing too much work on its main thread.
    01-18 21:58:20.650 16746-16746/poke.pokeclub W/ResourceType: No package identifier when getting value for resource number 0x00000032
    01-18 21:58:20.667 16746-16746/poke.pokeclub D/AndroidRuntime: Shutting down VM
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime: FATAL EXCEPTION: main
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime: Process: poke.pokeclub, PID: 16746
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime: android.content.res.Resources$NotFoundException: String resource ID #0x32
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at android.content.res.Resources.getText(Resources.java:299)
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at android.widget.TextView.setText(TextView.java:4138)
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at poke.pokeclub.Principal$1.onClick(Principal.java:36)
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at android.view.View.performClick(View.java:4785)
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19884)
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:746)
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5343)
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    01-18 21:58:20.715 16746-16746/poke.pokeclub E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
  • 4

    Add the code to the question by clicking [Edit]

  • 3

    Adds logcat error for more information as well

  • Here’s the missing data...

1 answer

2


You are trying to assign a value int in the setText(), whereas this method is expected to receive a String or a int representing an id corresponding to an appeal, that in the case, is not found(so launches a Resources$Notfoundexception). Try changing the line:

life.setText(ataque - 50);

for:

life.setText(String.valueOf(ataque - 50));

References:

android.content.res.Resurces$Notfoundexception : String Resource ID #0x0

android.content.res.Resources$Notfoundexception: String Resource ID Fatal Exception in Main

http://developer.android.com/intl/pt-br/reference/android/widget/TextView.html

Browser other questions tagged

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