2015-11-23 107 views
0

當試圖將圖像填充到持有人的ImageView中或當我嘗試將持有人圖像視圖設置爲onclick偵聽器時,該應用崩潰。它實際上崩潰,如果我試圖做什麼與持有人的imageview相關。Android ListView適配器返回NullPointerException

圖片URL不爲空,我查

的ImageView的存在,我查

適配器代碼(它的重要位):

public class FeedAdapter extends BaseAdapter { 

    static private Activity activity; 
    private static LayoutInflater inflater = null; 
    ArrayList<ActivityTable> actList = new ArrayList<ActivityTable>(); 
    Holder holder; 

    public FeedAdapter(Activity a, ArrayList<ActivityTable> actList) { 
      activity = a; 
      inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      this.actList = actList; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

      View vi = convertView; 
      final ActivityTable act = actList.get(position); 

      if (vi == null) { 
        vi = inflater.inflate(R.layout.feed_single_picture, parent, false); 
        holder = new Holder(); 
        holder.media = (ImageView) vi.findViewById(R.id.postphoto); 
        //can't set an image to it either. it crashes. 
      } 
      else { 
        holder = (Holder) vi.getTag(); 
      } 

      //This is where it crashes 
      holder.media.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 

           //other on Click related Code 

         } 
      }); 
     return vi; 
    } 

    public static class Holder { 
      ImageView media; 
    } 
} 

爲什麼Holder的ImageView崩潰了嗎?我嘗試添加一個TextView,ImageButton等,所有這些都正常工作。只是這個特定的imageview崩潰。我究竟做錯了什麼?

編輯:崩潰日誌

FATAL EXCEPTION: main 
    Process: com.metalproject.metalprojectmobile, PID: 5737 
    java.lang.NullPointerException 
      at Adapter.FeedAdapter.getView(FeedAdapter.java:456) 
      at android.widget.AbsListView.obtainView(AbsListView.java:2689) 
      at android.widget.ListView.makeAndAddView(ListView.java:1801) 
      at android.widget.ListView.fillDown(ListView.java:697) 
      at android.widget.ListView.fillGap(ListView.java:661) 

以上線:456分 - > holder.media.setOnClickListener(新View.OnClickListener(){

+1

顯示崩潰日誌 –

+0

肯定。堅持@ρяσѕρєяK – Dinuka

+0

@ρяσѕρєяK編輯 – Dinuka

回答

0

您應該修改代碼如下所示:

if (vi == null) { 
    vi = inflater.inflate(R.layout.feed_single_picture, parent, false); 
    holder = new Holder(); 
    holder.media = (ImageView) vi.findViewById(R.id.postphoto); 
} 

並且還將它們從外部代碼中移除if if

+0

對不起,我檢查了我的不好,代碼與您給我的格式相同。編輯問題 – Dinuka

+0

你有沒有添加vi.setTag(holder);在if塊? –

+0

是的,我有夥伴 – Dinuka

0

修復將是這樣的:

public View getView(int position, View convertView, ViewGroup parent) { 
    View vi = convertView; 
    final ActivityTable act = actList.get(position); 

    if (vi == null) { 
     vi = inflater.inflate(R.layout.feed_single_picture, parent, false); 
     holder = new Holder(); 
     vi.setTag(holder); 
    }else { 
     holder = (Holder) vi.getTag(); 
    } 

    holder.media.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     } 
    }); 
    return vi; 
} 

static class Holder { 

    private ImageView media; 

    Holder(View view){ 
     this.media = (ImageView) view.findViewById(R.id.postphoto); 
    } 

} 
+0

設置網址和所有代碼如何?我應該在哪裏放? – Dinuka

+0

if/else塊後,您將擁有所有已定義的視圖,您可以在此設置。注意:使用畢加索或Glide – adnbsr