0

So I'm trying to build the base of a simple app where I can switch activities with a button click. I have this working fine for my main activity to my second activity; however, I can't get it to work going back to the main activity.

Currently I am getting this error when I click the button: java.lang.IllegalStateException: Could not find method NewBack(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'back'

Heres the code in the second activity:

package com.example.homework4;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class CreateQuestion extends AppCompatActivity {

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

    public void NewBack(View v){
        setContentView(R.layout.activity_main);
    }

}

Here is the code for the button in my xml file:

<Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="153dp"
        android:onClick="NewBack"
        android:text="GO BACK"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/submit" />

I have also tried setting an onClickListener with the same results. I'm sure this is an easy fix, I just can't seem to figure it out!

Zain
  • 37,492
  • 7
  • 60
  • 84
  • you need to set an ID for your button. See the example [here](https://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene) – Carson Fujita-Turnbull Jun 18 '21 at 15:31
  • How are you navigating to this `CreateQuestion` activity? The fact that you have `setContentView()` as your back navigation (that won't work) hints that there might be similar issue in the forward navigation too. – laalto Jun 18 '21 at 15:33
  • That was the soltuion! I had to use the intent on going forward as well. Sorry for the dumb question, I clearly don't understand android that well – Henry Gilbert Jun 18 '21 at 16:05

1 Answers1

1

If You want to navigate between activities you need to replace

public void NewBack(View v){
    setContentView(R.layout.activity_main);
}

with

public void newBack(View v){
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);

}

P.S. don't use Upercase in function name. So if the function is called newBack

change it also in XML file like:

<Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="153dp"
        android:onClick="newBack"
        android:text="GO BACK"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/submit" />
DoctorWho
  • 1,044
  • 11
  • 34