本文介绍了类型getIntent()是未定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从另一个活动的ByteArray通过意图是这样的:

 如果(查看== NULL){
        鉴于=新ImageView的(mContext);
    }
    捆绑额外= getIntent()getExtras()。
      字节[]的字节数组= extras.getByteArray(图片);
      位图default_b = BitmapFactory.de codeByteArray的(字节数组,0,byteArray.length);
      view.setImageBitmap(default_b);
    view.setImageResource(drawables.get(位置));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(新android.widget.GridView.LayoutParams(70,70));
    view.setTag(将String.valueOf(位置));
    返回视图。

但我得到的错误getIntent()是未定义的类型GridViewAdapter(这是我的基本适配器类的GridView)

我在这里创建的意图:

 意向意图=新意图(v.getContext(),GridViewAdapter.class);
                                intent.putExtra(图片报,字节);
                                。v.getContext()startActivity(意向);

如何解决这个问题?

增加:

下面是我的,我创建的意图完整的部分:

  Log.d(AppInfoAdapter,数据设置为显示);
    addCheckbox
            .setOnClickListener(新View.OnClickListener(){                @覆盖
                公共无效的onClick(视图v){
                    如果(addCheckbox.isChecked()){
                        的System.out.println(选中);
                        软件包管理系统下午= mContext.getPackageManager();
                        可绘制图标= NULL;
                        尝试{
                            图标= PM
                            .getApplicationIcon(entry.activityInfo.packageName);
                        }赶上(ē的NameNotFoundException){
                            // TODO自动生成catch块
                            e.printStackTrace();
                        }
                        可绘制default_icon = pm.getDefaultActivityIcon();
                        如果(图标的i​​nstanceof BitmapDrawable
                                &功放;&安培; default_icon的instanceof BitmapDrawable){
                            BitmapDrawable icon_bd =(BitmapDrawable)图标;
                            位图icon_b = icon_bd.getBitmap();
                            BitmapDrawable default_bd =(BitmapDrawable)下午
                                    .getDefaultActivityIcon();
                            位图default_b = default_bd.getBitmap();
                            如果(icon_b == default_b){
                                //它的默认图标
                                scaleDownBitmap(default_b,100,v.getContext());
                                Log.d(AppInfoAdapter,缩放位图选上);                                ByteArrayOutputStream流=新ByteArrayOutputStream();
                                default_b.com preSS(Bitmap.Com pressFormat.PNG,100,流);
                                字节[]的字节数组= stream.toByteArray();
                                Log.d(AppInfoAdapter,缩放位图阵列);                                意向意图=新意图(v.getContext(),GridViewAdapter.class);
                                intent.putExtra(图片报,字节);
                                。v.getContext()startActivity(意向);
                                Log.d(AppInfoAdapter,意图开始派位图);
                            }
                        }
                    }其他{
                        的System.out.println(取消选中);
                    }                }
            });


解决方案

getIntent()用来获得意图用于启动活动。既然你是不是在活动则没有意图来的get getIntent(),因为它说,是不是适配器类的功能。

使用在的活动,code 调用适配器类,并传递需要的数据类

I am getting a byteArray from another activity via Intent like this:

        if (view == null) {
        view = new ImageView(mContext);
    }
    Bundle extras = getIntent().getExtras();
      byte[] byteArray = extras.getByteArray("picture");
      Bitmap default_b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
      view.setImageBitmap(default_b);
    view.setImageResource(drawables.get(position));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
    view.setTag(String.valueOf(position));
    return view;

But I get the error that getIntent() is undefined for the type GridViewAdapter (this is in my base adapter class for a gridView)

I create the intent here:

Intent intent = new Intent(v.getContext(), GridViewAdapter.class);
                                intent.putExtra("picture", byteArray);
                                v.getContext().startActivity(intent);

How can I fix this error?

ADDED:

Here is my full part where I create the intent:

        Log.d("AppInfoAdapter", "Data Set 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.activityInfo.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
                                scaleDownBitmap(default_b, 100, v.getContext());
                                Log.d("AppInfoAdapter", "Scale Bitmap Chosen");

                                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                default_b.compress(Bitmap.CompressFormat.PNG, 100, stream);
                                byte[] byteArray = stream.toByteArray();
                                Log.d("AppInfoAdapter", "Scale Bitmap to Array");

                                Intent intent = new Intent(v.getContext(), GridViewAdapter.class);
                                intent.putExtra("picture", byteArray);
                                v.getContext().startActivity(intent);
                                Log.d("AppInfoAdapter", "Intent started to send Bitmap");
                            }
                        }
                    } else {
                        System.out.println("Un-Checked");
                    }

                }
            });
解决方案

getIntent() is used to get the Intent used to start an Activity. Since you aren't in an Activity then there is no Intent to "get" and getIntent(), as it says, is not a function of the Adapter class.

Use that code in the Activity that calls the Adapter class and pass the data needed to that class

这篇关于类型getIntent()是未定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:11