Ajil O.
Ajil O.

Reputation: 6892

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.

Upvotes: 9

Views: 17296

Answers (3)

Sumit
Sumit

Reputation: 644

We need to define our method outside of the another func. like below

eg :

//another func

class MainActivity : AppCompatActivity() {      
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    
}

//my func

fun show_toast_msg(view:View) {   

    Toast.makeText(getApplicationContext(), "Im focused.", Toast.LENGTH_SHORT).show();

}

Upvotes: 0

Ajil O.
Ajil O.

Reputation: 6892

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.

Upvotes: 31

Tarsbir Singh
Tarsbir Singh

Reputation: 527

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

Upvotes: 3

Related Questions