按钮onClick()仅匹配mainActivity

时间:2018-04-21 15:44:03

标签: android android-activity

我在activity_welcome.xml

中有一个按钮
<?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"
    tools:context=".WelcomeActivity">

    <Button
        android:id="@+id/signIn"
        android:layout_width="270dp"
        android:layout_height="39dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:text="註冊"
        android:textColor="#000000"
        android:textSize="20sp"
        android:onClick="goToSignUpOne"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.602" />

</android.support.constraint.ConstraintLayout>

当我在WelcomeActivity类中调用我的goToSignUpOne方法时,它会导致崩溃,并且它似乎与该方法不匹配。这是goToSignUpOne函数的代码:

public void goToSignUpOne(View view) {
    setContentView(R.layout.activity_sign_up_one);
}

使用goToSignUpOne导航到我的MainActivity后,它可以正常工作! 所以我的按钮位于activity_welcome.xml但对应于MainActivity?是对的吗?我认为它应该与XML所针对的活动相对应。

错误日志

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.faketsao.dating, PID: 21004
                  java.lang.IllegalStateException: Could not find method goToSignUpOne(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'signIn'
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:380)
                      at android.view.View.performClick(View.java:6205)
                      at android.widget.TextView.performClick(TextView.java:11103)
                      at android.view.View$PerformClick.run(View.java:23653)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6682)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1534)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1424)

WelcomeActivity

package com.example.faketsao.dating;

        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;

public class WelcomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
    }

    public void goToSignUpOne(View view) {

        setContentView(R.layout.activity_sign_up_one);
    }
}

1 个答案:

答案 0 :(得分:0)

你在做什么是一种非常典型的方法。如果我是你,我会在WelcomeActivity的代码中定义onClick,而不是在XML中,因为使用XML很容易产生像你这样的错误。因此,您的WelcomeActivity的代码应该是这样的:

public class WelcomeActivity extends AppCompatActivity {

    private View.OnClickListener onButtonClick = new View.OnClickListener(){

        @Override
        public void onClick(View v){
            goToSignUpOne(v);
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        Button button = (Button) findViewById(R.id.signUp);
        button.setOnClickListener(onButtonClick);
    }

    public void goToSignUpOne(View view) {
        setContentView(R.layout.activity_sign_up_one);
    }
}

在XML中,删除按钮下的行android:onClick="goToSignUpOne",然后这应该有效。

如果您不想只更改视图,而是开始新活动,只需按照以下方式创建goToSignUpOne(View)函数:

public void goToSignUpOne(View view) {
    startActivity(new Intent(this, SignUpOneActivity.class));
}

希望这就是你要找的! :d