Android 在适配器之间传输映像

Android 在适配器之间传输映像,android,listview,gridview,android-adapter,Android,Listview,Gridview,Android Adapter,我有两个不同的适配器:一个用于gridView,另一个用于listView。在我的listView中,我有一个用户安装的应用程序列表,旁边有应用程序图标。在每个listView项目中,我也有一个复选框。选中此复选框后,我希望它获取特定应用程序的图标,然后将其发送到gridView,图标将显示在gridView中 问题是,当我单击复选框时,我得到以下NPE: FATAL EXCEPTION: main 12-22 15:58:45.782: E/AndroidRuntime(28793): jav

我有两个不同的适配器:一个用于gridView,另一个用于listView。在我的listView中,我有一个用户安装的应用程序列表,旁边有应用程序图标。在每个listView项目中,我也有一个复选框。选中此复选框后,我希望它获取特定应用程序的图标,然后将其发送到gridView,图标将显示在gridView中

问题是,当我单击复选框时,我得到以下NPE:

FATAL EXCEPTION: main
12-22 15:58:45.782: E/AndroidRuntime(28793): java.lang.NullPointerException
12-22 15:58:45.782: E/AndroidRuntime(28793):    at com.example.awesomefilebuilderwidget.AppInfoAdapter$1.onClick(AppInfoAdapter.java:200)
12-22 15:58:45.782: E/AndroidRuntime(28793):    at android.view.View.performClick(View.java:2532)
12-22 15:58:45.782: E/AndroidRuntime(28793):    at android.widget.CompoundButton.performClick(CompoundButton.java:99)
12-22 15:58:45.782: E/AndroidRuntime(28793):    at android.view.View$PerformClick.run(View.java:9308)
12-22 15:58:45.782: E/AndroidRuntime(28793):    at android.os.Handler.handleCallback(Handler.java:587)
12-22 15:58:45.782: E/AndroidRuntime(28793):    at android.os.Handler.dispatchMessage  (Handler.java:92)
12-22 15:58:45.782: E/AndroidRuntime(28793):    at android.os.Looper.loop(Looper.java:150)
12-22 15:58:45.782: E/AndroidRuntime(28793):    at android.app.ActivityThread.main(ActivityThread.java:4333)
12-22 15:58:45.782: E/AndroidRuntime(28793):    at java.lang.reflect.Method.invokeNative(Native Method)
12-22 15:58:45.782: E/AndroidRuntime(28793):    at java.lang.reflect.Method.invoke(Method.java:507)
12-22 15:58:45.782: E/AndroidRuntime(28793):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-22 15:58:45.782: E/AndroidRuntime(28793):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-22 15:58:45.782: E/AndroidRuntime(28793):    at dalvik.system.NativeStart.main(Native Method)
以下是复选框的onClick方法部分:

addCheckbox
            .setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (addCheckbox.isChecked()) {
                        System.out.println("Checked");

                        PackageManager pm = mContext.getPackageManager();
                        final int   DEST_IMAGE_WIDTH = 100;
                        final int DEST_IMAGE_HEIGHT = 100;
                        ApplicationInfo appInfo = mContext.getApplicationInfo();
                        Drawable appIcon = pm.getApplicationIcon(appInfo);
                        Bitmap appBmp  = Bitmap.createBitmap(DEST_IMAGE_WIDTH, DEST_IMAGE_HEIGHT, Config.ARGB_8888); 

                        // Creates a new canvas based on the image specification
                        // created just above.
                        Canvas canvas = new Canvas(appBmp);
                        // (optional) Fills the entire canvas
                        canvas.drawColor(Color.WHITE);
                        // You need to set bounds otherwise a 0,0 sized image would be drawn.
                        appIcon.setBounds(0, 0, DEST_IMAGE_WIDTH, DEST_IMAGE_HEIGHT);
                        appIcon.draw(canvas);

                        /// Let's save to a .jpg file ...
                        File file = new File(mContext.getFilesDir().getAbsolutePath() + "/test2.jpg");
                        FileOutputStream out;
                        try
                        {
                            file.createNewFile();
                            out = new FileOutputStream(file);
                            appBmp.compress(Bitmap.CompressFormat.JPEG, 80, out);
                            Log.i("AppInfoAdapter", "The icon for use in gridView is saved");
                            out.close();

                            // Load back the image file to confirms it works
                          Bitmap bitmap = BitmapFactory.decodeFile( file.getAbsolutePath() );
                          ImageView imageView1 = (ImageView)v.findViewById(R.id.iconImageView_id);
                          imageView1.setImageBitmap( bitmap );  
                          Log.i("AppInfoAdapter", "The icon image has been set into the gridView");
                        }

                        catch (FileNotFoundException e1)
                        {
                            e1.printStackTrace();
                        }
                        catch (IOException e2)
                        {
                            e2.printStackTrace();
                        }


                    } else {
                        System.out.println("Un-Checked");
                    }

                }
这是第200行:

ImageView imageView1 = (ImageView)v.findViewById(R.id.iconImageView_id);
下面是我的gridView适配器的getView()部分,我想在其中将图标图像放入视图:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    boolean checked = (mCheckBox==null)?false:(((CheckBox)  mCheckBox).isChecked());
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(Context);
        Log.d("GridViewAdapter", "new imageView added");
        view.setId(R.id.iconImageView_id);
    }
    if(checked == true){
        isSdReadable();
        Log.i("GridViewAdapter", "checkbox is checked");

/*FileInputStream in = null;
        try {
            in = Context.openFileInput("BitmapImage");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Load back the image file to confirm it works
        Bitmap bitmap = BitmapFactory.decodeStream(in);
        view.setImageBitmap(bitmap);
        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
    } else {
        Log.i("GridView", "Icons not for use/checkbox not checked");
    }

    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;
}
            });
我尝试为新视图分配一个ID,然后在图像中这样设置((在我的ONCLICK中注意:

ImageView imageView1 = (ImageView)v.findViewById(R.id.iconImageView_id);
在我看来:

view.setId(R.id.iconImageView_id);
)) 但这不起作用,只给了我一个NPE,因为两个适配器指向的视图不同

那么,有没有一种方法可以让他们提出相同的观点?(事实上,与我第一次想到这件事时相比,现在想想这件事

我可以在gridView适配器上使用FileinputStream来获取映像吗?如果可以,请告诉我如何进行此操作。我已经使用OpenfileInput等工具尝试过此操作,但一直无法使其正常工作。)

增加:

以下是listView适配器的整个getView()

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // get the selected entry
    final ResolveInfo entry = (ResolveInfo) mListAppInfo.get(position);

    // reference to convertView
    View v = convertView;

    // inflate new layout if null
    if (v == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        v = inflater.inflate(R.layout.layout_appinfo, null);
        Log.d("AppInfoAdapter", "New layout inflated");
    }

    // load controls from layout resources
    ImageView ivAppIcon = (ImageView) v.findViewById(R.id.ivIcon);
    TextView tvAppName = (TextView) v.findViewById(R.id.tvName);
    TextView tvPkgName = (TextView) v.findViewById(R.id.tvPack);
    final CheckBox addCheckbox = (CheckBox) v
            .findViewById(R.id.addCheckbox);
    Log.d("AppInfoAdapter", "Controls from layout Resources Loaded");

    // set data to display
    ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
    tvAppName.setText(entry.activityInfo.loadLabel(mPackManager));
    tvPkgName.setText(entry.activityInfo.packageName);

    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();
                        final int   DEST_IMAGE_WIDTH = 100;
                        final int DEST_IMAGE_HEIGHT = 100;
                        ApplicationInfo appInfo = mContext.getApplicationInfo();
                        Drawable appIcon = pm.getApplicationIcon(appInfo);
                        Bitmap appBmp  = Bitmap.createBitmap(DEST_IMAGE_WIDTH, DEST_IMAGE_HEIGHT, Config.ARGB_8888); 

                        // Creates a new canvas based on the image specification
                        // created just above.
                        Canvas canvas = new Canvas(appBmp);
                        // (optional) Fills the entire canvas
                        canvas.drawColor(Color.WHITE);
                        // You need to set bounds otherwise a 0,0 sized image would be drawn.
                        appIcon.setBounds(0, 0, DEST_IMAGE_WIDTH, DEST_IMAGE_HEIGHT);
                        appIcon.draw(canvas);

                        /// Let's save to a .jpg file ...
                        File file = new File(mContext.getFilesDir().getAbsolutePath() + "/test2.jpg");
                        FileOutputStream out;
                        try
                        {
                            file.createNewFile();
                            out = new FileOutputStream(file);
                            appBmp.compress(Bitmap.CompressFormat.JPEG, 80, out);
                            Log.i("AppInfoAdapter", "The icon for use in gridView is saved");
                            out.close();

                            // Load back the image file to confirms it works
                          Bitmap bitmap = BitmapFactory.decodeFile( file.getAbsolutePath() );
                          ImageView imageView1 = (ImageView)v.findViewById(R.id.iconImageView_id);
                          imageView1.setImageBitmap( bitmap );  
                          Log.i("AppInfoAdapter", "The icon image has been set into the gridView");
                        }

                        catch (FileNotFoundException e1)
                        {
                            e1.printStackTrace();
                        }
                        catch (IOException e2)
                        {
                            e2.printStackTrace();
                        }


                    } else {
                        System.out.println("Un-Checked");
                    }

                }
            });

    // return view
    return v;
}
增加: 新的NPE:

增加2: ids.xml


只需将
onClick
方法上的
v
名称更改为
v1
或任何其他名称,即可

View v=convertView

以及:

当您在onClick方法中使用
v
时,您在代码中使用了本地
v
,即
CheckBox
,在这种情况下,您就没有
v.findViewById()
,因为
v
参考
CheckBox
,所以您需要更改其中一个的名称

//更新

你有了新的NPE,就像你在201行看到的一样

imageView1.setImageBitmap( bitmap ); 
存在两种可能性:

1-位图为空

2-imageView1为空


首先检查
layout\u appinfo
上是否存在
iconImageView\u id
,如果存在,则检查位图

//更新2

正如我看到的,您尝试将映像从一个适配器添加到另一个适配器,因此您有2个选项

1-在网格适配器上完成这项工作(获取位图)(我不知道您到底想要什么,但您必须向该适配器发送一些内容以获取正确的位图)

2-在
GridAdapter
上获取一个
Bitmap
列表,并用它填充
Grid
,将
Bitmap
添加到该列表中,然后调用
GridAdapter.notifyDataSetChanged()
刷新
GridAdapter


必须为您提供秒解决方案。

请指出
AppInfoAdapter
的第200行。请检查您设置的更新问题单击listener了解什么
checkBox
或row?我为
checkBox
设置了侦听器,当单击该行时,它会执行其他操作
v.findViewById
错误,必须使用
yourView.findViewById()
而不是
checkBox.findViewById()
Ok,我将onClick方法中的
v
更改为v1,然后将v.findviewbyd更改为v1.findviewbyd并运行它,现在我在第201行(右下一行)上得到了一个NPE,请参见我的Edit@user2909006
iconImageView\u id
是我在gridView适配器中给新视图的id:
view.setId(R.id.iconImageView\u id)…我有一个
id值
部分,该部分显然要连接这些值?请检查更新的回答,因此从这里可能是iconImageView\u id返回空值是
iconImageView\u id
存在于
layout\u appinfo.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="iconImageView_id"/>
</resources>
public void onClick(View v) 
imageView1.setImageBitmap( bitmap );