Jay Di
Jay Di

Reputation: 19

OnClickListener doesn't work

My onClickListener is not working. Android Studio shows me the new View.onclickListener() and the cast in grey and I don’t know why ?

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

/**
 * Created by Jay Di on 30.07.2015.
 */
public class SignUpORLoginActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.log_in);



        //Log in Button Click Handler

     show me in gray  -->  ((Button) findViewById (R.id.button_anmelden)) .setOnClickListener(show me in gray too--->  new View.OnClickListener(){
           @Override
           public void onClick(View v) {

               //Starts an intent of the log in activity
               SignUpORLoginActivity.this.startActivity(new Intent(SignUpORLoginActivity.this, LogInActivity.class));
           }
       });

        //Sign up Button click handler
        ((TextView) findViewById(R.id.link_regestrieren)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Starts an intent of the sign up  activity
                SignUpORLoginActivity.this.startActivity(new Intent(SignUpORLoginActivity.this, SignUp.class));
            }
        });
    }


}

Isn’t just in this Class I have a fault in my Settings or Imports?

Upvotes: 0

Views: 6440

Answers (5)

Mostafa Imani
Mostafa Imani

Reputation: 554

For more details if you'r OnClickListener didn't work, check these ways below:

  1. check the id of button in layout and in findViewByID.(it's better to change and must be the same)

  2. check the priority of declaration and assignment and OnClickListener . (You must follow the priority of @Recomer said )

  3. Log the OnClickListener to find if it is working at all or just some part of it.

  4. check the item you called for your button is OnClickListener and the first argument is new View.OnClickListener() .

  5. check if it is truly a button . some views don't have Listener (as example :relative Layout) check if it is a button or imageView or some thing which has Listener of Click

  6. Cast your button for android.widget.Button as you can read answer of@Dharmaraj .

  7. At the end maybe your phone or the emulator is not responssing well. or some thing bad happen in your code. Log each line of your code (Clean the built project and rebuilt it )

I hope one of these ways solve your problem. (It is all depending experience and maybe it has more proper ways which i don't know)

Upvotes: 0

Beeing Jk
Beeing Jk

Reputation: 3926

Just to share my experience, try to avoid using common id which you can find in R.java...e.g. R.id.btnPlayImage, R.id.btnPlayMusic won't work.

Upvotes: 0

Jay Di
Jay Di

Reputation: 19

Hi the listener now works but if i start the app dosnt matter on emulator or real device i have the issue i become a black screen when i try to click the button_anmelden or link_regestrieren.

SignOrLogin: http://www.bilder-upload.eu/show.php?file=931f8e-1438636593.png

Upvotes: 0

Dharmaraj
Dharmaraj

Reputation: 1306

Its because you can't cast void type to android.widget.Button

Firstly you have to typecast this way

Button btn=(Button) findViewById (R.id.button_anmelden));

Then you can call OnclickListner on the btn like this

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Starts an intent of the sign up  activity
            SignUpORLoginActivity.this.startActivity(new Intent(SignUpORLoginActivity.this, SignUp.class));
        }
    });

Upvotes: 0

Recomer
Recomer

Reputation: 191

Did you try to first declare a Button before onCreate(...) and than assign this "button_anmelden" to it ? Like the code below.

public class SignUpORLoginActivity extends Activity {

    private Button loginButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.log_in);

        //Log in Button & Click Handler
        loginButton = (Button) findViewById(R.id.button_anmelden);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Starts an intent of the log in activity
                SignUpORLoginActivity.this.startActivity(new Intent(SignUpORLoginActivity.this, LogInActivity.class));
            }
        });

Can you give this code a try please ?

Upvotes: 1

Related Questions