2

When I assign the android:onclick in XML to the function, I get this error:

Error: Could not find method SubmitRegister(View) in a parent or ancestor Context for android:onClick attribute defined on view class

I checked my XML to see if there is a difference between the names, but I can assure that the function name has been spelled right.

Function in MainActivity.java:

  public void SubmitRegister(View v) {

    Log.d("MyApp", "Register button clicked.");

}

XML Content_main.XML:

   <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Registreren"
        android:id="@+id/buttonRegister"
        android:layout_below="@+id/inputpassword2"
        android:onClick="SubmitRegister"
        android:layout_marginTop="40dp"
        android:layout_alignEnd="@+id/inputpassword2"
        android:layout_alignStart="@+id/inputpassword2"
        android:enabled="true" />
fredsco
  • 313
  • 4
  • 19

1 Answers1

0

Make sure that your Activity's onCreate method is inflating your xml.

I usually add the onClick Listener on the java code and not on the xml. For example:

XML Content_main.xml :

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Registreren"
        android:id="@+id/buttonRegister"
        android:layout_below="@+id/inputpassword2"
        android:onClick="SubmitRegister"
        android:layout_marginTop="40dp"
        android:layout_alignEnd="@+id/inputpassword2"
        android:layout_alignStart="@+id/inputpassword2"
        android:enabled="true" />

JAVA MainActivity.java

Button btnRegister= (Button)findViewById(R.id.buttonRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    Log.d("MyApp", "Register button clicked.");
            }
        });