Question

I have created image gallery using database images. It has successfully displayed on screen. But while stretching the all image on the screen it creates null pointer exception. I have used BitmapFactory to Display image.

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

}

My Adpter class is:

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

Please help....

Was it helpful?

Solution

I solved this problem. in my database i found some null values, hence while accessing those data from bean my app was cracking. if you found same problem the be sure about metadata of database. Try to make attribute NOT NULL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top