0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

FIRST option get red line error and line 290 is : Toast.makeText(getApplicationContext(), "button clicked", Toast.LENGTH_LONG).show();}});
I'm sorry, that was a lot of code to look at. I have edited my answer. Use mContext
first option gave this red line error :No enclosing instance of the type DayGallery is accessible in scope
Did you change to mContext?
thanks alot , im trying to solve my problems step by step would you please take alook on my bounty question , thanks .;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.