Question

I want to create a simple app, that will have a button. I want to make it so that after the button has been clicked 10 times a message will pop up. but when i start an app, it doesn't work properly, i.e. I can push the button 20,30,40, etc. times and nothing will happen. What is wrong with my code? Thanks

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;


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();        
                }
            });
        }
    }       


}
Was it helpful?

Solution

I guess you forgot to call: gamebuild.show()

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