1

我关注了这个线程: OnCheckChanged 侦听器仅适用于 customlistview 中的第一个复选框

用于为我的 android 应用程序中的 CheckBox 编写 onCheckedListener() 代码。上面的链接有listview,在我的例子中我有一个gridview。

我目前的代码是:

public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder;
    ImageView imgView = null;

        if (convertView == null) {

            holder = new ViewHolder(); 
            LayoutInflater ltInflate = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); 
            convertView = ltInflate.inflate(R.layout.griditem, null);

            holder.textview1 = (TextView) convertView.findViewById(R.id.grid_item_alert_date);
            holder.textview2 = (TextView) convertView.findViewById(R.id.grid_item_alert_time);
            holder.textview3 = (TextView) convertView.findViewById(R.id.grid_item_alert_type);

            holder.imageview    = (ImageView) convertView.findViewById(R.id.grid_item_image);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox_ack);
            convertView.setTag(holder);

        }
        else
        {   
            holder = (ViewHolder) convertView.getTag();
        }

        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                 // TODO Auto-generated method stub
                  Toast.makeText(context, " checkbox checked", Toast.LENGTH_SHORT).show();

              } });

        holder.textview1.setId(position);
        holder.textview2.setId(position);
        holder.textview3.setId(position);
        holder.imageview.setId(position);
        holder.checkbox.setId(position);


        holder.textview1.setText("Text 1 ");
        holder.textview2.setText("Text 2 ");
        holder.textview3.setText("Text 3 ");
        holder.checkbox.setChecked(false);
        holder.imageview.setImageBitmap(bitmap);
        holder.id = position;



        return convertView;

}

当我选中我的应用程序崩溃的复选框时,我想知道为什么?


更新:这是选中复选框后的 logcat 输出:

10-08 23:01:54.777: I/inh(1950): Item loaded: /images/img1.jpg
10-08 23:02:03.538: D/AndroidRuntime(1950): Shutting down VM
10-08 23:02:03.538: W/dalvikvm(1950): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-08 23:02:03.557: E/AndroidRuntime(1950): FATAL EXCEPTION: main
10-08 23:02:03.557: E/AndroidRuntime(1950): java.lang.IllegalStateException: Could not find a method onCheckboxClicked(View) in the activity class com.exp.ir.client.ImageGraph for onClick handler on view class android.widget.CheckBox with id 'checkbox_ack'
10-08 23:02:03.557: E/AndroidRuntime(1950):     at android.view.View$1.onClick(View.java:3578)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at android.view.View.performClick(View.java:4084)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at android.widget.CompoundButton.performClick(CompoundButton.java:100)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at android.view.View$PerformClick.run(View.java:16966)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at android.os.Handler.handleCallback(Handler.java:615)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at android.os.Looper.loop(Looper.java:137)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at java.lang.reflect.Method.invokeNative(Native Method)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at java.lang.reflect.Method.invoke(Method.java:511)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at dalvik.system.NativeStart.main(Native Method)
10-08 23:02:03.557: E/AndroidRuntime(1950): Caused by: java.lang.NoSuchMethodException: onCheckboxClicked [class android.view.View]
10-08 23:02:03.557: E/AndroidRuntime(1950):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at java.lang.Class.getMethod(Class.java:915)
10-08 23:02:03.557: E/AndroidRuntime(1950):     at android.view.View$1.onClick(View.java:3571)
10-08 23:02:03.557: E/AndroidRuntime(1950):     ... 12 more
4

1 回答 1

3

好的,所以我在定义复选框的 layout.xml 中发现了问题我添加了这行导致找不到事件处理程序

android:onClick="onCheckboxClicked"

删除了这个,代码现在可以正常工作。

感谢 Sam 和其他人的观看。

于 2012-10-08T23:12:01.227 回答