Method has incorrect signature

Viewed 10398

Despite having the method onButtonHomeClick declared in the Java file MainActivity, when I attempt to reference the method in the XML I receive the error message: "Method onButtonHomeClick in MainActivity has incorrect signature". Both of them are below, and I cannot for the life of me figure out why it is returning such an error, especially since attempting to navigate to the declaration by Ctrl clicking onButtonHomeClick in the XML file navigates to the method in the Java class.
And it's definitely struggling to find the method, as the Android Monitor returns a fatal error when attempting to assign the behaviour to the button.

<!-- XML file -->
<item
        android:id="@+id/button_home"
        android:orderInCategory="100"
        android:icon="@drawable/home"
        android:title="Home"
        android:onClick="onButtonHomeClick"
        app:showAsAction="always"
    />

//Java method
public void onButtonHomeClick(View v){
    Intent intent = new Intent(this, MainActivity.class);

}
6 Answers

Clean your project and then make sure the method onButtonHomeClick() is declared as public void.

Your On Click Method has Invalid Arguments for MenuItem. For MenuItem, it is

public void onClickMethod(MenuItem menuItem) {
    // Your code here
}

Whereas public void onClickMethod(View view) {...} is for View present in your activity layout.

Also just creating an Intent will not start the Activity. For starting the activity you should pass your intent to startActivity().

in kotlin:

   <android.support.v7.widget.AppCompatButton
    android:id="@+id/mRecharge_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="onClick"
    android:textSize="18sp" />

in your activity create public fun onClick

  public fun onClick(view: View?) {
  // do ur logic here
}

You must also define the android: onClick method to all views. Remember that the click method name is the same in all views.

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layoutDirection="rtl" >

    <RadioButton
        android:id="@+id/radioButton_score"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Score"
        android:onClick="OnClickView"/>

    <RadioButton
        android:id="@+id/radioButton_discoun_percent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Discoun Percent"
        android:onClick="OnClickView" />
</RadioGroup>
public void OnClickView(View view) {
    switch(view.getId()){
        case R.id.radioButton_score:
            Toast.makeText(this, "click Score", 
                                Toast.LENGTH_SHORT).show();
            break;
        case R.id.radioButton_discoun_percent:
            Toast.makeText(this, "click Discoun Percent", 
                                 Toast.LENGTH_SHORT).show();
            break;
    }
}
Related