2013-04-13 55 views
0

我試着添加按鈕,我這圖庫視圖將Save按鈕保存圖像到SD卡上,運行應用程序時惜售顯示java.lang.NullPointerException:顯示java.lang.NullPointerException時添加按鈕圖庫查看

我的畫廊代碼:

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) 

任何建議將理解,由於

回答

1

試着改變你的Toast

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

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

我相信你需要用你的Activity context附上您的ToastActivity不是應用程序上下文

如果沒有按」 t修正它然後表明哪一行是290.這行在這裏logcat

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

說,事情是在null管線290

+0

第一個選項得到紅線錯誤和線路290:\t \t Toast.makeText(getApplicationContext() 「點擊按鈕」,Toast.LENGTH_LONG).show( );}}); – androidqq6

+0

對不起,這是很多代碼來看。我編輯了我的答案。使用'mContext' – codeMagic

+0

第一個選項給出了這個紅線錯誤:沒有可以在範圍內訪問DayGallery類型的封閉實例 – androidqq6