Question

I want to create a simple app in eclipse with a button. I want to make it so that after the button is pushed 10 times a message will pop up. The problem is that when I start the app and push the button 10 times , nothing happens. Could you please tell me what I've done wrong?

Here's my activity file:

package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class Game extends Activity implements android.view.View.OnClickListener{

    @Override 
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        Button gamebutton = (Button) findViewById(R.id.gamebutton);
    }
    @Override 
    public void onClick(View v){
        //TODO Auto-generated method stub
        int clicked = 0;
        clicked++;
        if(  clicked==10){
            AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
            gamebuild.setMessage("Good");
            gamebuild.setCancelable(false);
            gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Game.this.finish();

                }
            });
            gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
            dialog.cancel();        
                }
            });
        }

    }

}

Thanks for response! I've edited the activity file this way:

package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public class Game extends Activity implements android.view.View.OnClickListener{
    int clicked = 0;
    @Override 
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        Button gamebutton = (Button) findViewById(R.id.gamebutton);
    }

    @Override 
    public void onClick(View v){
        //TODO Auto-generated method stub

        clicked++;
        if(  clicked==10){
            AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
            gamebuild.setMessage("Good");
            gamebuild.setCancelable(false);
            gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Game.this.finish();

                }
            });
            gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
            dialog.cancel();        
                }
            });
        }

    }

}

Still it's not working in a proper way. Sorry for dumb questions: I'm new to android.

new edit:

package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public class Game extends Activity implements android.view.View.OnClickListener{
    int clicked = 0;
    @Override 
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

        Button gamebutton = (Button) findViewById(R.id.gamebutton);
        gamebutton.setOnClickListener(this);

    }

    @Override 
    public void onClick(View v){
        //TODO Auto-generated method stub

        clicked++;
        if(  clicked==10){
            AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
            gamebuild.setMessage("Good");
            gamebuild.setCancelable(false);
            gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Game.this.finish();

                }
            });
            gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
            dialog.cancel();        
                }
            });
        }

    }

}

No correct solution

OTHER TIPS

You put the counter inside the onClick function, causing it to reset every time. So, move

int clicked = 0;

from inside the onClick to before your onCreate such as:

int clicked = 0;
@Override
protected void onCreate....

This will make it set to 0 once, then stay equal to whatever it's last value was as long as the app is open and not killed.

Problem is that variable clicked is declared inside onClick. So it's always zero. You have to declare it globally, inside class Game.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top