I have a button in 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>
When I call my goToSignUpOne
method in the WelcomeActivity class, it causes a crash and it seems like which doesn't match the method. This is the code of the goToSignUpOne
function:
public void goToSignUpOne(View view) {
setContentView(R.layout.activity_sign_up_one);
}
After using goToSignUpOne
to navigate to my MainActivity, it works!
So my button is in the activity_welcome.xml
but corresponds to MainActivity? Is that right? I think it should rather correspond to the activity which the XML is for.
error log
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);
}
}