Tim Pungračič
Tim Pungračič

Reputation: 37

Android onClickListener works only once

So, I am very new to android programming. I want to make a simple application with three "attack" buttons, and when the user presses one button, it should calculate the damage, change the hp of one fo the players and output some text (who hurt who for how much). Everything works, but only for the first time. After the first button press the application doesn't respond to button presses anymore.

MainActivity.java

package sn.nujno.bitkadolgcasa;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;



public class MainActivity extends Activity implements View.OnClickListener {
Random rand = new Random();
public int hp1 = 100;
public int hp2 = 100;
public int power = 10;
public int igralec = 1;
public String text = "";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button nn = (Button)findViewById(R.id.nn);
    Button hn = (Button)findViewById(R.id.hn);
    Button mn = (Button)findViewById(R.id.mn);
    nn.setOnClickListener(this);
    hn.setOnClickListener(this);
    mn.setOnClickListener(this);
    }

    @Override        
    public void onClick(View v) { 
        int id = v.getId();
        int napad;
        int moznost;
        setContentView(R.layout.activity_main);
        TextView hpT=(TextView)findViewById(R.id.textView3);
        TextView hpA=(TextView)findViewById(R.id.textView4);
        TextView mw=(TextView)findViewById(R.id.mw);

        if(id == R.id.nn && igralec == 1){
        moznost = rand.nextInt(100);
            if(moznost >= 50){
                napad = rand.nextInt(10)+ 1;
                hp1 = hp1 - napad;
                text ="Tim je poskodoval Alena za " + napad + " zivljensjkih tock. Kaj bo storil Alen?" ;
            }else if(moznost < 50){
                text ="Tim je zgresil. Kaj bo storil Alen?";
        }
       igralec = 2;
       }

        else if(id == R.id.nn && igralec == 2){
        moznost = rand.nextInt(100);
            if(moznost >= 50){
                napad = rand.nextInt(10)+ 1;
                hp1 = hp1 - napad;
                text ="Alen je poskodoval Tima za " + napad + " zivljensjkih tock. Kaj bo storil Tim?" ;
            }else if(moznost < 50){
                text ="Alen je zgresil. Kaj bo storil Tim?";
            }
       igralec = 1;
       }

        else if(id == R.id.mn && igralec == 1){
            moznost = rand.nextInt(100);
            if(moznost >= 80){
                napad = rand.nextInt(15)+ 10;
                hp1 = hp1 - napad;
                text ="Tim je poskodoval Alena za " + napad + " zivljensjkih tock. Kaj bo storil Alen?" ;
            }else if(moznost < 80){
                text ="Tim je zgresil. Kaj bo storil Alen?";
            }
       igralec = 2;
       }

        else if(id == R.id.mn && igralec == 2){
            moznost = rand.nextInt(100);
            if(moznost >= 80){
                napad = rand.nextInt(15)+ 10;
                hp1 = hp1 - napad;
                text ="Alen je poskodoval Tima za " + napad + " zivljensjkih tock. Kaj bo storil Tim?" ;
            }else if(moznost < 80){
                text ="Alen je zgresil. Kaj bo storil Tim?";
            }
       igralec = 1;
       }

        else if(id == R.id.hn && igralec == 1){
            moznost = rand.nextInt(100);
            if(moznost >= 25){
                napad = rand.nextInt(5) +1;
                hp1 = hp1 - napad;
                text ="Tim je poskodoval Alena za " + napad + " zivljensjkih tock. Kaj bo storil Alen?" ;
            }else if(moznost < 25){
                text ="Tim je zgresil. Kaj bo storil Alen?";
        }
       igralec = 2;
       }

        else if(id == R.id.hn && igralec == 2){
            moznost = rand.nextInt(100);
            if(moznost >= 25){
                napad = rand.nextInt(5) +1;
                hp1 = hp1 - napad;
                text ="Alen je poskodoval Tima za " + napad + " zivljensjkih tock. Kaj bo storil Tim?" ;
            }else if(moznost < 25){
                text ="Alen je zgresil. Kaj bo storil Tim?";
        }
       igralec = 1;
       }

    else if(hp1 ==0){
        text = "Zmagal je Alen. Igra se bo začela znova.";
        hp1 = 100;
        hp2 = 100;
        igralec = 1;
        }
    else if(hp2 == 0){
        text = "Zmagal je Tim. Igra se bo začela znova.";
        hp1 = 100;
        hp2 = 100;
        igralec = 1;
        }

    mw.setText(text);
    hpT.setText(""+ hp1);
    hpA.setText(""+ hp2);
   }
}

Upvotes: 2

Views: 1932

Answers (2)

Eugen Pechanec
Eugen Pechanec

Reputation: 38223

In your onClick(View v) you call setContentView(R.layout.activity_main); which replaces all the buttons which already were in the Activity along with their onClickListeners.

Rewrite your onCreate method like this (and declare field variables):

private TextView hpT, hpA, mw;

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

    setContentView(R.layout.activity_main);

    Button nn = (Button)findViewById(R.id.nn);
    Button hn = (Button)findViewById(R.id.hn);
    Button mn = (Button)findViewById(R.id.mn);
    nn.setOnClickListener(this);
    hn.setOnClickListener(this);
    mn.setOnClickListener(this);

    hpT = (TextView)findViewById(R.id.textView3);
    hpA = (TextView)findViewById(R.id.textView4);
    mw = (TextView)findViewById(R.id.mw);
}

and remove the following lines from the onClick method:

setContentView(R.layout.activity_main);
TextView hpT=(TextView)findViewById(R.id.textView3);
TextView hpA=(TextView)findViewById(R.id.textView4);
TextView mw=(TextView)findViewById(R.id.mw);

This is for effectivity. The findViewById call is time consuming and the found View does not change between clicks - you can find it once in onCreate.

Upvotes: 0

laalto
laalto

Reputation: 152807

Remove the

setContentView(R.layout.activity_main);

from your onClick().

You're re-inflating the same layout and the new layout instance doesn't have the onclick listeners installed.

Upvotes: 1

Related Questions