but that code show Nullpointer error
it's print mes "after image adpater" then show error "Nullpointer"
Using java Syntax Highlighting
- public class demo2 extends View{
- private Paint mPaint = new Paint();
- private Context ctx;
- private Canvas mCanvas = new Canvas();
- private int[] myImageIds = {
- R.drawable.image_1,
- R.drawable.image_2,
- R.drawable.image_3,
- R.drawable.image,
- R.drawable.ena,
- R.drawable.image_4
- };
- public demo2(Context context) {
- // TODO Auto-generated constructor stub
- super(context);
- ctx = context;
- this.setBackground(getResources().getDrawable(R.drawable.b));
- setFocusable(true);
- // ((Gallery) findViewById(R.id.gallery)).setAdapter(new ImageAdapter(this.ctx));
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- mCanvas = canvas;
- int screen_hei=mCanvas.getBitmapHeight();
- int screen_wid=mCanvas.getBitmapWidth();
- mPaint.setARGB(255,255,255,255);
- mCanvas.drawRoundRect(new RectF(10,10,(screen_wid-10),screen_hei/5), 5, 5, mPaint);
- mCanvas.drawRoundRect(new RectF(10,screen_hei/5+10,(screen_wid-10),screen_hei-30), 5, 5, mPaint);
- Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.xs2);
- int image_hei=image.height();
- int image_wei=image.width();
- canvas.drawBitmap(image,(screen_wid-image_wei)/2,(screen_hei/5-image_hei)/2, mPaint);
- Gallery gallery = (Gallery) findViewById(R.id.gallery);
- Log.i("canvas","Gallery Gallery Gallery Gallery ");
- ImageAdapter IA=new ImageAdapter(this.ctx);
- Log.i("Canvas","after image adpater ");
- gallery.setAdapter(IA);
- Log.i("canvas","ImageAdapterImageAdapterImageAdapter ");
- }
- public class ImageAdapter extends BaseAdapter{
- private Context mContext;
- public ImageAdapter(Context context1) {
- // TODO Auto-generated constructor stub
- mContext=context1;
- }
- public int getCount() {
- // TODO Auto-generated method stub
- return myImageIds.length;
- }
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- public float getScale(boolean focused, int offset) {
- return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
- }
- public float getAlpha(boolean focused, int offset) {
- return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- ImageView i = new ImageView(this.mContext);
- i.setTag(i.getResources().getText(myImageIds[position]));
- i.setImageResource(myImageIds[position]);
- i.setScaleType(ImageView.ScaleType.FIT_XY);
- i.setLayoutParams(new Gallery.LayoutParams(150, 150));
- return i;
- }
- }
- }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4





