简体   繁体   English

OnClickListener 不起作用

[英]OnClickListener doesn't work

My onClickListener is not working.我的 onClickListener 不工作。 Android Studio shows me the new View.onclickListener() and the cast in grey and I don't know why ? Android Studio 向我展示了新的View.onclickListener()和灰色的演员表,我不知道为什么?

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 ?您是否尝试在 onCreate(...) 之前先声明一个 Button 然后将这个“button_anmelden”分配给它? 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这是因为你不能将 void 类型转换为 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 上调用 OnclickListner

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.嗨,监听器现在可以工作了,但是如果我在模拟器或真实设备上启动应用程序无关紧要,我会遇到问题,当我尝试单击 button_anmelden 或 link_regestrieren 时,我会变成黑屏。

SignOrLogin:http://www.bilder-upload.eu/show.php?file=931f8e-1438636593.png登录或登录: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:有关更多详细信息,如果您的 OnClickListener 不起作用,请检查以下方法:

  1. check the id of button in layout and in findViewByID.(it's better to change and must be the same)检查布局和findViewByID中按钮的id。(最好更改并且必须相同)

  2. check the priority of declaration and assignment and OnClickListener .检查声明和分配的优先级以及 OnClickListener 。 (You must follow the priority of @Recomer said ) (你必须遵循@Recomer 所说的优先级)

  3. Log the OnClickListener to find if it is working at all or just some part of it.记录 OnClickListener 以查看它是完全正常工作还是仅部分工作。

  4. check the item you called for your button is OnClickListener and the first argument is new View.OnClickListener() .检查您为按钮调用的项目是 OnClickListener 并且第一个参数是 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某些视图没有侦听器(例如:相对布局)检查它是否是按钮或 imageView 或某些具有 Click 侦听器的东西

  6. Cast your button for android.widget.Button as you can read answer of@Dharmaraj .为 android.widget.Button 投射您的按钮,因为您可以阅读@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) (这完全取决于经验,也许它有更多我不知道的正确方法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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