gpt4 book ai didi

android - oncheckedlistener 破坏代码 android

转载 作者:行者123 更新时间:2023-11-29 18:07:10 25 4
gpt4 key购买 nike

我关注了这个话题: 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

最佳答案

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

android:onClick="onCheckboxClicked"

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

感谢 Sam 和其他人的观看。

关于android - oncheckedlistener 破坏代码 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12790489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com