0

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);
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Hsiu Chuan Tsao
  • 1,396
  • 1
  • 12
  • 23
  • 1
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [ask] page for help clarifying this question. – ADM Apr 21 '18 at 15:45
  • 1
    Post the log error please. – Augusto Apr 21 '18 at 15:46
  • 2
    You use `startActivity(new Intent(this, MainActivity.class))` to start a new Activity. Don't use `setContentView` to change your activity, because that only changes the inflated XML. – Ian Rehwinkel Apr 21 '18 at 15:51
  • Please attach your `WelcomeActivity` – ישו אוהב אותך Apr 21 '18 at 15:55
  • 1
    I think the answer of @I.Renk is right, I change `setContentView ` to `startActivity(new Intent(this, MainActivity.class))` then it works. It seems like that my activity still at MainActivity and never transfer to WelcomeActivity even thought the view changed. – Hsiu Chuan Tsao Apr 21 '18 at 15:59

1 Answers1

0

What you are doing is a pretty non-typical approach. If I were you, I would define the onClick in the code of WelcomeActivity, and not in the XML, since using the XML is prone to produce errors like yours. Therefore, the code of your WelcomeActivity should be something like this:

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);
    }
}

In the XML, you remove the line android:onClick="goToSignUpOne" under your Button, and then that should work.

In case you didn't only want to change the view, but start a new activity instead, just make the goToSignUpOne(View) function like this:

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

Hope this is what you were looking for! :D

Ian Rehwinkel
  • 2,486
  • 5
  • 22
  • 56