您好,我正在尝试使用此代码在gridview中显示来自图片网址的图片 它显示空指针异常可以任何人帮助我: 我的代码是
public class Gridview extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter{
private Context mContext;
private Integer[] mThumbIds;
public ImageAdapter(Context c)
{
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(45, 45));
imageView.setAdjustViewBounds(false);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView
.setImageDrawable(LoadImageFromWebOperations("http://tmcrmappsqa.inservices.tatamotors.com/cordys/Images/mobile.png"+position));
return imageView;
}
protected Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.exit(0);
return null;
}
}
}
答案 0 :(得分:0)
嘿检查你的图片下载网址。为什么你在这个网址上附加了位置?如果您有很多图像,如mobile1,mobile2等,那么您应该在移动后追加位置,而不是在网址后追加。
最重要的是,你不能像现在这样在主UI线程上进行网络操作。它可能适用于GingerBread和Pre-GingerBread设备,但不适用于以后的GingerBread设备。
请参考此link并以此方式执行此操作。
答案 1 :(得分:0)
你应该检查网址,+位置会导致错误的网址:
imageView.setImageDrawable(LoadImageFromWebOperations("http://tmcrmappsqa.inservices.tatamotors.com/cordys/Images/mobile.png"+position));