Nemeium
Nemeium

Reputation: 43

Android can't find my OnClick method

I have an issue with this botton, when is clicked the app send a message "ImageButton has stopped". I think it's something dumb, but I just started coding. Details of my code are below.

LogCat:

12-13 21:01:56.659 1075-1075/com.example.christian.imagebutton E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.christian.imagebutton, PID: 1075
                                                                             java.lang.IllegalStateException: Could not find method llamar(MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageButton with id 'boton1'
                                                                                 at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
                                                                                 at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                                 at android.view.View.performClick(View.java:5637)
                                                                                 at android.view.View$PerformClick.run(View.java:22429)
                                                                                 at android.os.Handler.handleCallback(Handler.java:751)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Main Activity

package com.example.christian.imagebutton;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView tv1;

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

    tv1=(TextView)findViewById(R.id.tv1);

}
public void llamar(View view){
    tv1.setText("Llamando");
    }
}

And the layout is:

<RelativeLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.christian.imagebutton.MainActivity">

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@mipmap/telefono"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/boton1"
    android:onClick="llamar(MainActivity)" />

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/boton1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="22dp"
    android:id="@+id/tv1" />
</RelativeLayout>

Upvotes: 1

Views: 5047

Answers (2)

Charuka Silva
Charuka Silva

Reputation: 13153

Android just implements the OnClickListener for you when you define the android:onClick="someMethod" attribute.

Those two code snippets are totally the same but just implemented in two different ways.

Code Implementation

Button btn = (Button) findViewById(R.id.mybutton);

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        llamar(v);
    }
});

// some more code

public void llamar(View v) {
    // does something very interesting
}

Above is a code implementation of an OnClickListener. And now the XML implementation.

XML Implementation

<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="@+id/mybutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:onClick="llamar" />
<!-- even more layout elements -->

Now in the background Android does nothing else than the Java code calling your method on a click event.

Note that with the XML above, Android will look for the onClick method llamar() only in the current Activity. This is important to remember if you are using fragments, since even if you add the XML above using a fragment, Android will not look for the onClick method in the .java file of the fragment used to add the XML.

Upvotes: 0

Denominator
Denominator

Reputation: 216

android:onClick="llamar" 

Shouldn't it be like this?

Upvotes: 1

Related Questions