我试图在我的galleryview中添加按钮,当运行应用程序java.lang.NullPointerException时,该按钮将保存图像到sd卡:
我的画廊代码:
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; } }
日志猫:
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)
任何建议将不胜感激,谢谢