findViewById使用imageView返回NULL

时间:2013-12-23 02:17:54

标签: android nullpointerexception

我有一个gridViewAdapter类,当没有视图时会创建一个视图,我为它设置了一个Id。以下是该类的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");
    } 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;
}

我的资源的值部分中有一个ids.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <item type="id" name="iconImageView_id"/>
</resources>

然后我想在这里找到另一个班级的imageView

/// 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");
}

但我在这一行得到NPE

imageView1.setImageBitmap( bitmap ); 

告诉我imageView正在返回null

这是为什么?我该如何解决这个问题?

增加:

我在另一个适配器类中获取了imageView:

@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;
}

0 个答案:

没有答案