Thanks Andreas, issue was solved but onDraw is not caling and because
of this my full image is not showing, below is my changed code
package com.example.HelloGridView;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class HelloGridView extends Activity{
int current_pos;
public MyView myView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myView = new MyView(this);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int
position, long id)
{
Toast.makeText(HelloGridView.this, "" + position,
Toast.LENGTH_SHORT).show();
current_pos = position;
myView.invalidate();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the
Adapter
public View getView(int position, View convertView, ViewGroup
parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled,
initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new
GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_0
};
}
public class MyView extends View {
private Bitmap[] mTileArray;
private final Paint mPaint = new Paint();
public MyView(Context context) {
super(context);
//TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.TileView);
// mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
//a.recycle();
}
public MyView(Context context, AttributeSet attrs, int
defStyle) {
super(context, attrs, defStyle);
//TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.TileView);
// mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
//a.recycle();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void initresources()
{
Resources r = this.getContext().getResources();
loadTile(0, r.getDrawable(R.drawable.sample_0));
loadTile(1, r.getDrawable(R.drawable.sample_1));
loadTile(2, r.getDrawable(R.drawable.sample_2));
loadTile(3, r.getDrawable(R.drawable.sample_0));
}
public void loadTile(int key, Drawable tile) {
Bitmap bitmap = Bitmap.createBitmap(191, 285,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tile.setBounds(0, 0, 191, 285);
tile.draw(canvas);
mTileArray[key] = bitmap;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mTileArray[1], 0,0, mPaint);
}
}
}
On Apr 1, 2:29 pm, Andreas Agvard <[email protected]>
wrote:
> myView is never initialized and thus null
> myView.invalidate(); causes NullPointerException as a result
>
> On Apr 1, 11:09 am, mack2978 <[email protected]> wrote:
>
>
>
> > logs:
> > Thread [<1> main] (Suspended (exception NullPointerException))
> > HelloGridView$1.onItemClick(AdapterView, View, int, long) line: 38
> > GridView(AdapterView).performItemClick(View, int, long) line: 284
> > AbsListView$PerformClick.run() line: 1696
> > ViewRoot(Handler).handleCallback(Message) line: 587
> > ViewRoot(Handler).dispatchMessage(Message) line: 92
> > Looper.loop() line: 123
> > ActivityThread.main(String[]) line: 4627
> > Method.invokeNative(Object, Object[], Class, Class[], Class, int,
> > boolean) line: not available [native method]
> > Method.invoke(Object, Object...) line: 521
> > ZygoteInit$MethodAndArgsCaller.run() line: 868
> > ZygoteInit.main(String[]) line: 626
> > NativeStart.main(String[]) line: not available [native method]
>
> > On Apr 1, 1:54špm, Kostya Vasilyev <[email protected]> wrote:
>
> > > Logcat output?
> > > 01.04.2011 12:04 ÐÏÌØÚÏ×ÁÔÅÌØ "mack2978" <[email protected]> ÎÁÐÉÓÁÌ:
>
> > > > Hi All,
>
> > > > I am trying to show full image when clicking on one of the thumbnail
> > > > of grid view, i am able to view the grid with thumbnails but as soon
> > > > as i click on any thumbnail to show it in full screen, handling
> > > > onItemClick(), it crashes. below is my complete code, please help
>
> > > > package com.example.HelloGridView;
>
> > > > import android.app.Activity;
> > > > import android.os.Bundle;
> > > > import android.content.Context;
> > > > import android.content.res.Resources;
> > > > import android.graphics.Bitmap;
> > > > import android.graphics.Canvas;
> > > > import android.graphics.Paint;
> > > > import android.graphics.drawable.Drawable;
> > > > import android.util.AttributeSet;
> > > > import android.view.View;
> > > > import android.view.ViewGroup;
> > > > import android.widget.AdapterView;
> > > > import android.widget.AdapterView.OnItemClickListener;
> > > > import android.widget.BaseAdapter;
> > > > import android.widget.GridView;
> > > > import android.widget.ImageView;
> > > > import android.widget.Toast;
>
> > > > public class HelloGridView extends Activity{
> > > > int current_pos;
> > > > private Bitmap[] mTileArray;
> > > > private MyView myView;
> > > > /** Called when the activity is first created. */
> > > > @Override
> > > > public void onCreate(Bundle savedInstanceState) {
> > > > super.onCreate(savedInstanceState);
> > > > setContentView(R.layout.main);
> > > > GridView gridview = (GridView) findViewById(R.id.gridview);
> > > > gridview.setAdapter(new ImageAdapter(this));
> > > > gridview.setOnItemClickListener(new OnItemClickListener() {
> > > > public void onItemClick(AdapterView<?> parent, View v, int
> > > > position, long id)
> > > > {
> > > > Toast.makeText(HelloGridView.this, "" + position,
> > > > Toast.LENGTH_SHORT).show();
> > > > current_pos = position;
> > > > myView.invalidate();
> > > > }
> > > > });
>
> > > > }
>
> > > > public class ImageAdapter extends BaseAdapter {
> > > > private Context mContext;
>
> > > > public ImageAdapter(Context c) {
> > > > mContext = c;
> > > > }
>
> > > > public int getCount() {
> > > > return mThumbIds.length;
> > > > }
>
> > > > public Object getItem(int position) {
> > > > return null;
> > > > }
>
> > > > public long getItemId(int position) {
> > > > return 0;
> > > > }
>
> > > > // create a new ImageView for each item referenced by the
> > > > Adapter
> > > > public View getView(int position, View convertView, ViewGroup
> > > > parent) {
> > > > ImageView imageView;
> > > > if (convertView == null) { // if it's not recycled,
> > > > initialize some attributes
> > > > imageView = new ImageView(mContext);
> > > > imageView.setLayoutParams(new
> > > > GridView.LayoutParams(85, 85));
>
> > > > imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
> > > > imageView.setPadding(8, 8, 8, 8);
> > > > } else {
> > > > imageView = (ImageView) convertView;
> > > > }
>
> > > > imageView.setImageResource(mThumbIds[position]);
> > > > return imageView;
> > > > }
>
> > > > // references to our images
> > > > private Integer[] mThumbIds = {
> > > > R.drawable.sample_0, R.drawable.sample_1,
> > > > R.drawable.sample_2, R.drawable.sample_0
> > > > };
> > > > }
>
> > > > public class MyView extends View {
>
> > > > private Bitmap[] mTileArray;
> > > > private final Paint mPaint = new Paint();
>
> > > > public MyView(Context context, AttributeSet attrs, int
> > > > defStyle) {
> > > > super(context, attrs, defStyle);
>
> > > > //TypedArray a = context.obtainStyledAttributes(attrs,
> > > > R.styleable.TileView);
> > > > // mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
> > > > //a.recycle();
> > > > Resources r = this.getContext().getResources();
> > > > loadTile(0,
> > > > r.getDrawable(R.drawable.sample_0));
> > > > loadTile(1, r.getDrawable(R.drawable.sample_1));
> > > > loadTile(2, r.getDrawable(R.drawable.sample_2));
> > > > loadTile(3, r.getDrawable(R.drawable.sample_0));
> > > > }
>
> > > > public MyView(Context context, AttributeSet attrs) {
> > > > super(context, attrs);
>
> > > > Resources r = this.getContext().getResources();
> > > > loadTile(0,
> > > > r.getDrawable(R.drawable.sample_0));
> > > > loadTile(1, r.getDrawable(R.drawable.sample_1));
> > > > loadTile(2, r.getDrawable(R.drawable.sample_2));
> > > > loadTile(3, r.getDrawable(R.drawable.sample_0));
> > > > }
>
> > > > public void loadTile(int key, Drawable tile) {
> > > > Bitmap bitmap = Bitmap.createBitmap(191, 285,
> > > > Bitmap.Config.ARGB_8888);
> > > > Canvas canvas = new Canvas(bitmap);
> > > > tile.setBounds(0, 0, 191, 285);
> > > > tile.draw(canvas);
> > > > mTileArray[key] = bitmap;
> > > > }
>
> > > > @Override
> > > > public void onDraw(Canvas canvas) {
> > > > super.onDraw(canvas);
> > > > canvas.drawBitmap(mTileArray[1], 0,0, mPaint);
>
> > > > }
>
> > > > }
>
> > > > }
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > > > Groups "Android Developers" group.
> > > > To post to this group, send email to [email protected]
> > > > To unsubscribe from this group, send email to
> > > > [email protected]
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/android-developers?hl=en-Hidequoted text -
>
> > > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en