onClick on Android

2

I made a EditText with the text = 100, attribute but when I use my onClick() function, which is to do the following text - 50 calculation, when I click the button and execute the routine my application closes. / p>

Follow 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 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)
    
asked by anonymous 18.01.2016 / 22:42

1 answer

2

You are trying to assign a int value to setText() , this method expects get a String or a int representing an id corresponding to a resource , which in this case is not found (so it throws a Resources$NotFoundException ). Try changing the line:

life.setText(ataque - 50);

To:

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

link

    
19.01.2016 / 01:11