简体   繁体   中英

how to check whether Button is clicked or not

I have to check whether the button is clicked or not.if clicked application has to do one task and if not, application has to do another task. I tried to do this, but I am getting no connection error which I have put at the end of the code in catch block.

 protected void onCreate(Bundle savedInstanceState) {
    PracticeVO practiceObj;

    try {

        setTitle("Klarity");
        setPrefBtn = (Button) findViewById(R.id.setPrefBtn);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_klarity_home);

        /*
         * asynchronous calls
         */

        StrictMode.ThreadPolicy policy = new     StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        final ConnectionHelper con = new ConnectionHelper();
        /*
         * It will connect to DB and fetches the Practice Information
         */
        if (Btnclicked == false) {

            String allPracticesStr = null;

here I have set the boolean variable 'Btnclicked' true.

 setPrefBtn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Btnclicked=true;
                    Intent setPrefIntent = new Intent(KlarityHome.this,
                            SetPreferences.class);
                    startActivity(setPrefIntent);

                }
            });

But After executing this The cursor is directly goin here and displaying 'no connection'.

catch (Exception ex) {
        Context context = getApplicationContext();
        CharSequence text = "There is some error in application";
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        System.out.println("no connection");
    }

}

Anyone has solution on this.

as you said

"the button is clicked or not.if clicked application has to do one task and if noapplication has to do another task" so, if i were you, i'll put 2 radio buttons, every one with the text of the task you want to do, and add a listener for them, like this:

radiobutton1.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            radiobutton2.setChecked(false);
        }
    });

radiobutton2.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                radiobutton1.setChecked(false);
            }
    });

and last, in the button do the task you want to do:

    Button.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {
    if(radiobutton1.isChecked){
    //do task 1
}else{
//do task2}
    }
    });

You are getting an NPE(NullPointerException) since you havent initialised Btnclicked in your code at the time of usage.ie you are checking whether Btnclicked is false like

if (Btnclicked == false) {

But at the time of this checking, the value of Btnclicked is null and hence an NPE.

So to get rid of this, you just add either

Btnclicked=true;

or

Btnclicked=false;

before the if loop according to your logic.

OR

or just replace the declaration from Boolean Btnclicked; to Boolean Btnclicked=false;

Hope this helps.

Remove Btnclicked you don't need that. OnClickListner is meant for what you're trying to do. The code which you want to run when button is not pressed put it before

//When button is not pressed
setPrefBtn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //When button is pressed
                }
            });

And the code which you want to run when the button is clicked place it inside the onClick(){}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM