简体   繁体   中英

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?

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 ?

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

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

只是为了分享我的经验,尽量避免使用您可以在 R.java 中找到的公共 ID...例如R.id.btnPlayImageR.id.btnPlayMusic将不起作用。

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)

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