Android onClick方法不工作

Android onClick方法不工作,android,Android,我有一个带有两个按钮的登录页面,每个按钮指向不同的活动。。onClick方法均未运行。。我知道这应该是非常基本的东西,但我找不到解决办法 我的代码如下: public class LandingPage extends AppCompatActivity { Button log_in, sign_up; Typeface tfc_button; @Override protected void onCreate(Bundle savedInstanceState) { reques

我有一个带有两个按钮的登录页面,每个按钮指向不同的活动。。onClick方法均未运行。。我知道这应该是非常基本的东西,但我找不到解决办法

我的代码如下:

public class LandingPage extends AppCompatActivity {

Button log_in, sign_up;
Typeface tfc_button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_landing_page);

    log_in = (Button) findViewById(R.id.LogIn_Button);
    sign_up = (Button) findViewById(R.id.SignUp_Button);

    setFontType();
    addSoundtoButtons();

}

public void addSoundtoButtons(){
    //Add Sound to the Buttons
    final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.button_click_sound);
    log_in.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mediaPlayer.start();
        }
    });
    sign_up.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mediaPlayer.start();
        }
    });
}

public void setFontType(){

    //Set Font Type for Buttons
    tfc_button = Typeface.createFromAsset(getAssets(), "fonts/TEMPSITC.TTF");

    log_in = (Button) findViewById(R.id.LogIn_Button);
    sign_up = (Button) findViewById(R.id.SignUp_Button);

    log_in.setTypeface(tfc_button);
    sign_up.setTypeface(tfc_button);

}

public void OnClickButtonLogin(View view){
    Intent intent = new Intent(this, SignInPage.class);
    startActivity(intent);}

public void OnClickButtonSignUp(View view){
    Intent intent = new Intent(this, SignUpPage.class);
    startActivity(intent);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/landingpagelandscape"
    tools:context="com.example.gebruiker.prototype1oneplayerpurevisualisation.Activities.LandingPage">

    <Button
        android:id="@+id/LogIn_Button"
        android:layout_width="171dp"
        android:layout_height="57dp"
        android:layout_marginBottom="25dp"
        android:layout_marginStart="100dp"
        android:background="@drawable/login_button"
        android:onClick="OnClickButtonLogin"
        android:text="Log In"
        android:textColor="#FFEAEA"
        android:textSize="35sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/SignUp_Button"
        android:layout_width="171dp"
        android:layout_height="57dp"
        android:layout_marginBottom="25dp"
        android:onClick="OnClickButtonSignUp"
        android:layout_marginEnd="100dp"
        android:background="@drawable/signup_button"
        android:text="Sign Up"
        android:textColor="#FFEAEA"
        android:textSize="35sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>
我的活动布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/landingpagenormal"
    tools:context="com.example.gebruiker.prototype1oneplayerpurevisualisation.Activities.LandingPage">

    <Button
        android:id="@+id/LogIn_Button"
        android:layout_width="171dp"
        android:layout_height="57dp"
        android:layout_marginBottom="17dp"
        android:layout_marginStart="18dp"
        android:background="@drawable/login_button"
        android:onClick="OnClickButtonLogin"
        android:text="Log In"
        android:textColor="#FFEAEA"
        android:textSize="35sp"
        app:layout_constraintBottom_toTopOf="@+id/SignUp_Button"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/SignUp_Button"
        android:layout_width="171dp"
        android:layout_height="57dp"
        android:onClick="OnClickButtonSignUp"
        android:layout_marginBottom="38dp"
        android:layout_marginStart="18dp"
        android:background="@drawable/signup_button"
        android:text="Sign Up"
        android:textColor="#FFEAEA"
        android:textSize="35sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

我的活动的景观模式布局如下:

public class LandingPage extends AppCompatActivity {

Button log_in, sign_up;
Typeface tfc_button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_landing_page);

    log_in = (Button) findViewById(R.id.LogIn_Button);
    sign_up = (Button) findViewById(R.id.SignUp_Button);

    setFontType();
    addSoundtoButtons();

}

public void addSoundtoButtons(){
    //Add Sound to the Buttons
    final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.button_click_sound);
    log_in.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mediaPlayer.start();
        }
    });
    sign_up.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mediaPlayer.start();
        }
    });
}

public void setFontType(){

    //Set Font Type for Buttons
    tfc_button = Typeface.createFromAsset(getAssets(), "fonts/TEMPSITC.TTF");

    log_in = (Button) findViewById(R.id.LogIn_Button);
    sign_up = (Button) findViewById(R.id.SignUp_Button);

    log_in.setTypeface(tfc_button);
    sign_up.setTypeface(tfc_button);

}

public void OnClickButtonLogin(View view){
    Intent intent = new Intent(this, SignInPage.class);
    startActivity(intent);}

public void OnClickButtonSignUp(View view){
    Intent intent = new Intent(this, SignUpPage.class);
    startActivity(intent);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/landingpagelandscape"
    tools:context="com.example.gebruiker.prototype1oneplayerpurevisualisation.Activities.LandingPage">

    <Button
        android:id="@+id/LogIn_Button"
        android:layout_width="171dp"
        android:layout_height="57dp"
        android:layout_marginBottom="25dp"
        android:layout_marginStart="100dp"
        android:background="@drawable/login_button"
        android:onClick="OnClickButtonLogin"
        android:text="Log In"
        android:textColor="#FFEAEA"
        android:textSize="35sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/SignUp_Button"
        android:layout_width="171dp"
        android:layout_height="57dp"
        android:layout_marginBottom="25dp"
        android:onClick="OnClickButtonSignUp"
        android:layout_marginEnd="100dp"
        android:background="@drawable/signup_button"
        android:text="Sign Up"
        android:textColor="#FFEAEA"
        android:textSize="35sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

好的,那么,您有两件相互冲突的事情。首先,布局中的
onClick
属性:

其次,活动中的
setOnClickListener()

第二个将覆盖第一个。您应该仍然可以看到/听到
mediaPlayer.start()
的效果,但您永远不会得到调用
OnClickButtonLogin()
。如果您想同时使用这两种方法,可以删除
setOnClickListener()
调用,只需将
mediaPlayer.start()
添加到
OnClickButtonLogin()
方法:

public void OnClickButtonLogin(View view) {
    mediaPlayer.start();

    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

从您发布的代码来看,没有任何问题(当我将其复制到IDE时,一切正常)。您是否遗漏了可能影响按钮点击的内容?例如,如果(出于某种原因)您调用了
log\u in.setOnClickListener(null)
,那么这就可以解释为什么它不起作用了。我添加了省略的代码,并按照建议进行了修改。。谢谢