onClick方法无法运行Android

时间:2018-03-29 22:16:02

标签: android

我有一个带有两个按钮的LandingPage,每个按钮指向不同的活动..没有一个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/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>

我的活动的横向模式布局如下:

<?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>

1 个答案:

答案 0 :(得分:1)

好的,你有两件相互矛盾的事情。首先,布局中的cursor.execute(statement, parameters) sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user_event_details [SQL: "SELECT distinct(user_id) FROM user_event_details WHERE age >=19 AND granular_timestamp >=1493596800 AND granular_timestamp <=1517356800 AND event_name ='IB_TRANS' GROUP BY user_id,event_id;'] (Background on this error at: http://sqlalche.me/e/e3q8) 属性:

onClick

其次,您的活动中的android:onClick="OnClickButtonLogin"

setOnClickListener()

第二个将覆盖第一个。你仍然应该看到/听到log_in.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mediaPlayer.start(); } }); 的效果,但你不会接到mediaPlayer.start()的电话。如果您想同时使用这两种方式,则可以删除OnClickButtonLogin()来电并将setOnClickListener()添加到mediaPlayer.start()方法:

OnClickButtonLogin()