androidqq6
androidqq6

Reputation: 1526

java.lang.NullPointerException when add button to gallery view

Im trying to add button to my galleryview which will be save button to save images to sd card , when run app java.lang.NullPointerException rised :

my gallery code :

class InfiniteGalleryAdapter extends BaseAdapter { 
private Context mContext;
private int[] images;   
private String[] name;
public InfiniteGalleryAdapter(Context c, int[] imageIds,String[] names) { 
this.mContext = c; 
images = imageIds;
name=names;
inflater = (LayoutInflater)mContext.getSystemService (
Context.LAYOUT_INFLATER_SERVICE); }

public int getCount() { 
return Integer.MAX_VALUE; } 

public Object getItem(int position) { 
return position; } 

public long getItemId(int position) { 
return position; } 

private LayoutInflater inflater=null; 

public class ViewHolder{ 
public TextView text; 
public ImageView image; 
public Button button;}

public View getView(int position, View convertView, ViewGroup parent) { 
ImageView i = getImageView(); 

int itemPos = (position % images.length); 

try { i.setImageResource(images[itemPos]); ((BitmapDrawable)
i.getDrawable()).setAntiAlias(true); } 

catch (OutOfMemoryError e) { Log.e("InfiniteGalleryAdapter", "Out of
memory creating imageview. Using empty view.", e); } 

view vi=convertView; 
ViewHolder holder; 
if(convertView==null){ 
vi = inflater.inflate(R.layout.gallery_items, null); 
holder=new ViewHolder(); 
holder.text=(TextView)vi.findViewById(R.id.textView1); 
holder.image=(ImageView)vi.findViewById(R.id.image); 

////// HERE WHERE TO ADD BUTTON ////


holder.button=(Button)vi.findViewById(R.id.button_save);
holder.button.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

Toast.makeText(getApplicationContext(), "button clicked", Toast.LENGTH_LONG).
show();}});

vi.setTag(holder); } 

else holder=(ViewHolder)vi.getTag(); 
holder.text.setText(name[itemPos]); 

final int stub_id=images[itemPos]; 
holder.image.setImageResource(stub_id); 

return vi; } 

private ImageView getImageView() { 

ImageView i = new ImageView(mContext); 

return i; } }

LOGCAT:

   FATAL EXCEPTION: main
        java.lang.NullPointerException
  at android.widget.Toast.<init>(Toast.java:90)
  at android.widget.Toast.makeText(Toast.java:232)
  at com.test.demo.InfiniteGalleryAdapter$1.onClick(DayGallery.java:290)
  at android.view.View.performClick(View.java:2485)
  at android.view.View$PerformClick.run(View.java:9080)
  at android.os.Handler.handleCallback(Handler.java:587)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:130)
  at android.app.ActivityThread.main(ActivityThread.java:3687)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:507)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
          (ZygoteInit.java:867)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
  at dalvik.system.NativeStart.main(Native Method)

ANY ADVICE WILL BE APPRECIATED , THANKS

Upvotes: 0

Views: 1020

Answers (1)

codeMagic
codeMagic

Reputation: 44571

Try changing your Toast from

Toast.makeText(mContext, "button clicked", Toast.LENGTH_LONG).
show();

to

Toast.makeText(DayGallery .this, "button clicked", Toast.LENGTH_LONG).
show();

I believe you need to use your Activity context to attach your Toast to your Activity not Application context

If this doesn't fix it then indicate which line is 290. This line here in logcat

at com.test.demo.InfiniteGalleryAdapter$1.onClick(DayGallery.java:290)

says that something is null at line 290

Upvotes: 1

Related Questions