oncheckedlistener打破了代码android

时间:2012-10-08 22:41:26

标签: android checkbox

我跟着这个帖子: The OnCheckChanged listener works only for the first checkbox in a 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

1 个答案:

答案 0 :(得分:3)

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

机器人:的onClick = “onCheckboxClicked”

删除了此代码,现在代码正常运行。

感谢Sam和其他人的观看。