Android 无法从 ListView Row 中按钮的 onClick 中找到方法 [英] Android cannot find method from button's onClick in ListView Row

查看:10
本文介绍了Android 无法从 ListView Row 中按钮的 onClick 中找到方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从自定义适配器填充的 ListView.每行有 1 个按钮.在 xml 中,按钮传递了 onClick 属性.我只有 xml,没有设置任何 OnClickListeners.另请注意,我的 CustomActivity 中存在 public void myMethod (View v).我得到以下异常

I have a ListView populated from a custom adapter. Each row has 1 button in it. In the xml the button has the onClick attribute passed. I have only the xml, not any OnClickListeners set. Also note that the public void myMethod (View v) exists in my CustomActivity. I get the following exception

10-02 03:01:46.463: E/AndroidRuntime(26857): java.lang.IllegalStateException: Could not find a method myClickHandler(View) in the activity class **android.app.Application** for onClick handler on view class android.widget.Button with id 'myButton'

Activity中的方法:

Method in Activity:

public void myClickHandler(View v) {
    ... do stuff here...
}

按钮 XML:

        <Button 
            android:id="@+id/myButton"
            android:layout_width="44dp"
            android:layout_height="44dp"
            android:background="@drawable/eye_icon"
            android:onClick="myClickHandler"
            />

异常中的一个有趣的注释是应用程序尝试在 android.app.Application 中而不是在我的自定义 Activity 中查找方法.

One Interesting note in the Exception is that the app tries to find the method in android.app.Application and not in my custom Activity.

有什么建议吗?

推荐答案

重要的是 CustomAdapterMyActivitygetContext() 必须是同一个例子.把你的和我的比较一下.

It’s important that MyActivity and getContext() of CustomAdapter must be the same instance. Compare yours with mine.

我的代码:

MyActivity.java

MyActivity.java

public class MyActivity extends Activity {
    public static final String TAG = "MyActivity";
    private ListView mListView;
    private CustomAdapter mAdapter;
    private ArrayList<String> mData;

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

        mListView = (ListView) findViewById(R.id.listView);

        mData = new ArrayList<String>();
        mData.add("111");
        mData.add("222");
        mData.add("333");
        mData.add("444");
        mData.add("555");

        mAdapter = new CustomAdapter(this, R.layout.list_item_view, mData);
        mListView.setAdapter(mAdapter);
    }

    public void onClickHandler(View view) {
        Log.i(TAG, "onClickHandler()");
    }
}

CustomAdapter.java

CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<String> {

    public CustomAdapter(Context context, int resource, ArrayList<String> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item_view, null);
        }

        return convertView;
    }
}

activity_my.xml

activity_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

list_item_view.xml

list_item_view.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:id="@+id/button"
        android:onClick="onClickHandler"/>
</LinearLayout>

这篇关于Android 无法从 ListView Row 中按钮的 onClick 中找到方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆