我是编码和java的新手并且遇到了问题并且无法修复它。 奇怪的是,错误只发生在android 5上。
以下是错误消息
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.nostra13.universalimageloader.core.ImageLoader.displayImage(java.lang.String, android.widget.ImageView, com.nostra13.universalimageloader.core.DisplayImageOptions)' on a null object reference
at com.fragments.activity.StoreActivity$1.OnMGArrayAdapterCreated(StoreActivity.java:143)
at com.adapters.MGArrayAdapter.getView(MGArrayAdapter.java:82)
StoreActivity.java
public void OnMGArrayAdapterCreated (MGArrayAdapter adapter, View v,
int position, ViewGroup viewGroup, Object obj){
// TODO Auto-generated method stub
final Store store = arrayData.get(position);
Photo p = q.getPhotoByStoreId(store.getStore_id());
MGImageView imgViewPhoto = (MGImageView) v.findViewById(R.id.imgViewPhoto);
imgViewPhoto.setCornerRadius(0.0f);
imgViewPhoto.setBorderWidth(UIConfig.BORDER_WIDTH);
imgViewPhoto.setBorderColor(getResources().getColor(UIConfig.THEME_BLACK_COLOR));
imgViewPhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(StoreActivity.this, DetailActivity.class);
i.putExtra("store", store);
StoreActivity.this.startActivity(i);
}
});
if (p != null) {
MainActivity.getImageLoader().displayImage(p.getPhoto_url(), imgViewPhoto, options);
}
else {
imgViewPhoto.setImageResource(UIConfig.SLIDER_PLACEHOLDER);
}
}
MGArrayAdapter.java
public class MGArrayAdapter extends BaseAdapter {
private Context c;
private int resId;
private OnMGArrayAdapterListener mCallback;
public List<Object> objects;
public interface OnMGArrayAdapterListener {
public void OnMGArrayAdapterCreated(MGArrayAdapter adapter, View v, int position, ViewGroup viewGroup, Object obj);
}
public void setOnMGArrayAdapterListener(OnMGArrayAdapterListener listener) {
try {
mCallback = (OnMGArrayAdapterListener) listener;
}
catch (ClassCastException e) {
throw new ClassCastException(this.toString() + " must implement OnMGArrayAdapterListener");
}
}
public MGArrayAdapter(Context c, ArrayList<Object> objects, int resId) {
this.c = c;
this.objects = objects;
this.resId = resId;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return objects.size();
}
@Override
public Object getItem(int pos) {
// TODO Auto-generated method stub
return objects.get(pos);
}
@Override
public long getItemId(int pos) {
// TODO Auto-generated method stub
return pos;
}
@Override
public View getView(int pos, View v, ViewGroup viewGroup) {
// TODO Auto-generated method stub
ViewHolder viewHolder = null;
if (v == null) {
LayoutInflater li = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(resId, null);
viewHolder = new ViewHolder();
viewHolder.view = v;
v.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) v.getTag();
Log.w("MGListAdapter Class", "View being reused.");
}
if (mCallback != null)
mCallback.OnMGArrayAdapterCreated(this, viewHolder.view, pos, viewGroup, objects.get(pos));
return v;
}
public class ViewHolder {
public View view;
}
public void add(Object obj) {
if (objects == null)
objects = new ArrayList<Object>();
objects.add(obj);
}
public void clearAll() {
if (objects == null)
objects = new ArrayList<Object>();
objects.clear();
}
}
答案 0 :(得分:0)
为什么不尝试:
public void add(Object obj) {
if(objects == null)
objects = new ArrayList<Object>();
objects.add(obj);
}
public void clearAll(Object obj) {
if(objects == null)
objects = new ArrayList<Object>();
objects.clear();
}
答案 1 :(得分:0)
在StoreActivity.java中,行MainActivity.getImageLoader()
以某种方式返回null,然后在其上面调用dispalyImage
,这会导致空指针异常。请检查静态方法getImageLoader
的代码,以了解其返回null的原因或在此处发布代码,以便我们可以帮助您找出答案。