简体   繁体   English

Android无法找到我的OnClick方法

[英]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". 我有这个问题,当点击该应用程序发送消息“ImageButton已停止”。 I think it's something dumb, but I just started coding. 我认为这是愚蠢的,但我刚刚开始编码。 Details of my code are below. 我的代码详情如下。

LogCat: 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>
android:onClick="llamar" 

不应该这样吗?

Android just implements the OnClickListener for you when you define the android:onClick="someMethod" attribute. 当您定义android:onClick="someMethod"属性时,Android只为您实现OnClickListener

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 . 以上是OnClickListener的代码实现。 And now the XML implementation. 现在是XML实现。

XML Implementation XML实现

<?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. 现在在后台Android中除了在单击事件上调用方法的Java代码之外什么也没做。

Note that with the XML above, Android will look for the onClick method llamar() only in the current Activity. 请注意,使用上面的XML,Android将仅在当前的Activity中查找onClick方法llamar() 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. 如果您正在使用fragments ,这一点很重要,因为即使您使用fragments添加上面的XML,Android也不会在用于添加XML的片段的.java文件中查找onClick方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM