【问题标题】:android Image Gallery exceptionandroid 图片库异常
【发布时间】:2026-02-08 12:40:02
【问题描述】:

我已经使用数据库图像创建了图像库。它已成功显示在屏幕上。但是在拉伸屏幕上的所有图像时,它会创建空指针异常。 我已经使用 BitmapFactory 来显示图像。

public class HotelGallery extends Activity {

GridView gv;
Button nextpg;
Context context;

ArrayList prgmName;

HotelDBHandler hdb = new HotelDBHandler(this);

public String[] prgmNameList2 = new String[100];
public byte[][] prgmImages2 = new byte[100][];

public static String[] prgmNameList = { "Hotel 1", "Hotel 2", "Hotel 3",
        "Hotel 4", "Hotel 5", "Hotel 6", "Hotel 7", "Hotel 8", "Hotel 9",
        "Hotel 1", "Hotel 2", "Hotel 3", "Hotel 4", "Hotel 5", "Hotel 6",
        "Hotel 7", "Hotel 8", "Hotel 9" };

public static int[] prgmImages = { R.drawable.hotel2, R.drawable.hotel3,
        R.drawable.hotel4, R.drawable.hotel5, R.drawable.hotel6,
        R.drawable.hotel2, R.drawable.hotel3, R.drawable.hotel4,
        R.drawable.hotel5, R.drawable.hotel2, R.drawable.hotel3,
        R.drawable.hotel4, R.drawable.hotel5, R.drawable.hotel6,
        R.drawable.hotel2, R.drawable.hotel3, R.drawable.hotel4,
        R.drawable.hotel5 };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.hotelgallary);

    gv = (GridView) findViewById(R.id.gridView1);
    nextpg = (Button) findViewById(R.id.btNext);

    List<HotelBean> items = hdb.getAllHotels();

    for (int i = 0; i < items.size(); i++) {
        prgmNameList2[i] = items.get(i).getName().toString();
        prgmImages2[i] = items.get(i).getHimg();

    }

    // prgmNameList2[0]=items.get(11).getName().toString();
    // Toast.makeText(getApplicationContext(),prgmNameList2[1].toString(),
    // Toast.LENGTH_LONG).show();

    nextpg.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent i = new Intent(HotelGallery.this, HotelTypes.class);
            startActivity(i);

        }
    });
    gv.setAdapter(new HotelGalleryAdapter(HotelGallery.this, prgmNameList2,
            prgmImages2));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

我的适配器类是:

public class HotelGalleryAdapter extends BaseAdapter {
 View rowView=null;
String[] result;
Context context;
byte[][] imageId;
  private static LayoutInflater inflater;
  byte[] outImage=null;
  ImageView img2;
  ByteArrayInputStream imageStream=null;


public HotelGalleryAdapter(HotelGallery mainActivity,
        String[] prgmNameList, byte[][] prgmImages) {
    // TODO Auto-generated constructor stub
    result = prgmNameList;
    context = mainActivity;
    imageId = prgmImages;
    inflater= (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder {
    TextView tv;
    ImageView img;
}

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();



             rowView = inflater.inflate(R.layout.imglist, null);
             holder.tv=(TextView) rowView.findViewById(R.id.textView1);
             holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

         holder.tv.setText(result[position]);

      // convert byte to bitmap take from contact class


         outImage = imageId[position];
        imageStream = new ByteArrayInputStream(outImage);
        Bitmap theImage = BitmapFactory.decodeStream(imageStream);
        holder.img.setImageBitmap(theImage);
        img2.setImageBitmap(theImage);
    //    Drawable d1 =new BitmapDrawable(context.getResources(),theImage);
         //holder.img.setImageResource(imageId[position]);

         rowView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
            }
        });

        return rowView;
    }
}

请帮忙....

【问题讨论】:

    标签: android image bitmapfactory


    【解决方案1】:

    我解决了这个问题。 在我的数据库中,我发现了一些空值,因此在从 bean 访问这些数据时,我的应用程序正在破解。 如果您发现同样的问题,请确定数据库的元数据。尝试使属性 NOT NULL。

    【讨论】:

    • 如果您认为您的数据库可以包含空值,请始终执行空检查操作
    • 是的!感谢您的建议。