Laughing_Man
Laughing_Man

Reputation: 179

Could not find method in a parent or ancestor Context

Problem

I am getting java.lang.IllegalStateException: Could not find method secondOne(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton

What I've done?

In manifest, I have activities:

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
    </activity>

    <activity
        android:name=".LeadActivity"
        android:label="@string/app_name">
    </activity>

    <activity
        android:name=".MainMenu"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

In game_menu.xml, I have two buttons. First has android:onClick="firstOne" property, second has android:onClick="secondOne" property.

In class MainMenu, which extends AppCompatActivity I have two methods:

public void firstOne(View view) {
    Intent intent = new Intent(MainMenu.this, MainActivity.class);
    startActivity(intent);
}

public void secondOne(View view) {
    Intent intent = new Intent(MainMenu.this, LeadActivity.class);
    startActivity(intent);
}

When I tap on the first button, my MainActivity.class runs, as it should.

But when I tap on the second button this error appears. java.lang.IllegalStateException: Could not find method secondOne(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton

Please, help. What am I doing wrong?

UPD

full MainMenu class code:

public class MainMenu extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_menu);

    //Hide the status bar and the action bar
    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
}

public void firstOne(View view) {
    Intent intent = new Intent(MainMenu.this, MainActivity.class);
    startActivity(intent);
}

public void secondOne(View view) {
    Intent intent = new Intent(MainMenu.this, LeadActivity.class);
    startActivity(intent);
}}

UPD2

game_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/sky"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="-16dp" />

    <Button
        android:id="@+id/button_start"
        android:layout_width="175dp"
        android:layout_height="76dp"
        android:layout_marginTop="200dp"
        android:background="@color/black"
        android:onClick="firstOne"
        android:text="@string/playButtonText"
        android:textColor="@color/white"
        android:textSize="40sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/imageView"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_lead"
        android:layout_width="175dp"
        android:layout_height="76dp"
        android:layout_marginTop="312dp"
        android:background="@color/black"
        android:onClick="secondOne"
        android:text="@string/leaderboardButtonText"
        android:textColor="@color/white"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/imageView"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 3

Views: 2794

Answers (1)

Laughing_Man
Laughing_Man

Reputation: 179

That was terrible one. I've spent a lot of time on this, but the answer was simple. Always care about onCreate method and actually setContentView(R.layout.); line. I've been copy-pasting and tried to create classes with the same layout.

Upvotes: 1

Related Questions