0

我正在尝试在复选框的 onClick 方法中使用意图,但我收到错误,即方法 startActivity 未定义并且构造函数 Intent(new View.OnClickListener ect..) 也未定义。这是我的代码:

        final CheckBox addCheckbox = (CheckBox) v
            .findViewById(R.id.addCheckbox);

    // set data to display

    addCheckbox
            .setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (addCheckbox.isChecked()) {
                        System.out.println("Checked");
                        PackageManager pm = mContext.getPackageManager();
                        Drawable icon = null;
                        try {
                            icon = pm
                                    .getApplicationIcon(entry.packageName);
                        } catch (NameNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        Drawable default_icon = pm.getDefaultActivityIcon();
                        if (icon instanceof BitmapDrawable
                                && default_icon instanceof BitmapDrawable) {
                            BitmapDrawable icon_bd = (BitmapDrawable) icon;
                            Bitmap icon_b = icon_bd.getBitmap();
                            BitmapDrawable default_bd = (BitmapDrawable) pm
                                    .getDefaultActivityIcon();
                            Bitmap default_b = default_bd.getBitmap();
                            if (icon_b == default_b) {
                                // It's the default icon


                                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                default_b.compress(Bitmap.CompressFormat.PNG, 100, stream);
                                byte[] byteArray = stream.toByteArray();

                                Intent intent = new Intent(this, GridView.class);
                                intent.putExtra("picture", byteArray);
                                this.startActivity(intent);
                            }
                        }
                    } else {
                        System.out.println("Un-Checked");
                    }

                }
            });
4

2 回答 2

1

用下面的代码替换你的代码

Intent intent = new Intent(YOURACTIVITY_NAME.this, GridView.class);

intent.putExtra("picture", byteArray);

 startActivity(intent);
于 2013-11-01T06:02:41.903 回答
0

由于您在 a 内listener,因此this指的listener是您收到错误的原因

“方法 startActivity 未定义”

只需删除this

startActivity(intent);

这与另一个错误的原因相同

构造函数 Intent(new View.OnClickListener 等..)"

使用被点击的ViewsContext这将是Activity Context

Intent intent = new Intent(v.getContext(), GridView.class);

所以那个块看起来像

Intent intent = new Intent(v.getContext(), GridView.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
于 2013-10-31T23:08:21.100 回答