Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Corresponding method handler not found - Android XML

I came across this error when creating making changes to the XML of an existing project. This is what my XML looked like

activity_month_view.xml

<android.support.design.widget.CoordinatorLayout
    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=".activities.YearViewActivity">

    <ImageView
        android:id="@+id/month_view_prev_month"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_keyboard_arrow_left_black_24dp"
        android:onClick="changeMonth"/>

</android.support.design.widget.CoordinatorLayout>

My Java class

MonthViewActivity.java

public class MonthViewActivity extends AppCompatActivity {

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

    public void changeMonth(View view) {
        //Some methods here
    }

}

The onClick property showed this error

 Corresponding method handler 'public void changeMonth(android.view.View)' not found

I had a look at this and this but they did not help.

like image 938
Ajil O. Avatar asked Dec 12 '17 10:12

Ajil O.


2 Answers

You can try this to solve the issue

XML in .MainActivity

 <TextView
    android:id="@+id/numbers"
    style="@style/CategoryStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/category_numbers"
    android:text="@string/category_numbers"
    android:onClick="openNumberList"/>

Java Code

public class MainActivity extends AppCompatActivity {

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

    public void openNumberList(View view) {
        Intent i = new Intent(this, NumberActivity.class);
        startActivity(i);
    }
}

declare a method in MainActivity with the name of openNumberList. You don't need to define it in NumberActitvity

like image 84
Tarsbir Singh Avatar answered Nov 04 '22 01:11

Tarsbir Singh


I had made a mistake in declaring the context in the XML. I had used .activities.YearViewActivity but the changeMonth method was declared in a different activity, which was causing the error.

<android.support.design.widget.CoordinatorLayout
    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=".activities.MonthViewActivity"> // Changed here

    <ImageView
        android:id="@+id/month_view_prev_month"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_keyboard_arrow_left_black_24dp"
        android:onClick="changeMonth"/>

</android.support.design.widget.CoordinatorLayout>

Changing the tools:context to the class of the calling activity fixed the error for me.

like image 23
Ajil O. Avatar answered Nov 04 '22 00:11

Ajil O.