【问题标题】:Android cannot find method from button's onClick in ListView RowAndroid 无法从 ListView Row 中按钮的 onClick 中找到方法
【发布时间】:2014-10-02 00:10:02
【问题描述】:

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

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中的方法:

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 中查找方法。

有什么建议吗?

【问题讨论】:

    标签: android listview button onclick


    【解决方案1】:

    MyActivitygetContext()CustomAdapter 必须是同一个实例,这一点很重要。把你的和我的比较一下。

    我的代码:

    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

    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

    <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

    <?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>
    

    【讨论】:

    • 你是对的,虽然我已经找到了答案。我在适配器中传递了“getApplicationContext()”结果而不是我的自定义活动......非常感谢
    【解决方案2】:

    在创建自定义Adapter 时,我将getApplicationContext() 方法的结果作为Context 传递。这是错误的。我应该将this(我的自定义Activity)作为Context 传递。它现在就像一个魅力。

    【讨论】:

      【解决方案3】:

      我假设您为此视图编写了一个自定义适配器,因此在您的适配器中调用 getView 时只需在按钮上 findElementById 并在那里设置 onClickListener。

      【讨论】:

      • 假设 OP 已经编写了自定义适配器可能是不安全的。但是,建议他这样做对我来说似乎是个好主意。
      【解决方案4】:

      这是onClick 属性被视为损坏的一个示例。最好创建一个自定义适配器并在getView() 方法中手动设置OnClickListener

      【讨论】:

        猜你喜欢
        • 2014-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-07
        • 2014-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多