我使用数据库图像创建了图库。它已成功显示在屏幕上。但是在拉伸屏幕上的所有图像时,它会创建空指针异常。 我使用了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;
}
}
我的Adpter课程是:
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;
}
}
请帮忙......
答案 0 :(得分:0)
我解决了这个问题。 在我的数据库中,我发现了一些空值,因此当从bean访问这些数据我的应用程序崩溃。 如果你发现同样的问题,请确保数据库的元数据。尝试使属性NOT NULL。