我正在借助Universal Image loader库构建应用程序。我的主要活动从一些网址加载图片并在gridview中显示。我想在单独的活动中显示完整的图像。当我点击任何图像在新活动中打开它时,我的应用程序崩溃了。
我无法理解UIL给出的示例代码,它很冗长。所以,我想只进行网格视图活动。
这是我的代码:
ImageListAdapter.java
public class ImageListAdapter extends BaseAdapter {
public String[] urls = {
"https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
"https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
"https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
"https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s1024/Antelope%252520Butte.jpg",
"https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s1024/Antelope%252520Hallway.jpg",
"https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s1024/Antelope%252520Walls.jpg",
"https://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpg",
"https://lh3.googleusercontent.com/-s-AFpvgSeew/URquc6dF-JI/AAAAAAAAAbs/Mt3xNGRUd68/s1024/Backlit%252520Cloud.jpg",
"https://lh5.googleusercontent.com/-bvmif9a9YOQ/URquea3heHI/AAAAAAAAAbs/rcr6wyeQtAo/s1024/Bee%252520and%252520Flower.jpg",
"https://lh5.googleusercontent.com/-n7mdm7I7FGs/URqueT_BT-I/AAAAAAAAAbs/9MYmXlmpSAo/s1024/Bonzai%252520Rock%252520Sunset.jpg",
"https://lh6.googleusercontent.com/-4CN4X4t0M1k/URqufPozWzI/AAAAAAAAAbs/8wK41lg1KPs/s1024/Caterpillar.jpg",
"https://lh3.googleusercontent.com/-rrFnVC8xQEg/URqufdrLBaI/AAAAAAAAAbs/s69WYy_fl1E/s1024/Chess.jpg",
};
private Context context;
private ImageLoader imageLoader;
public ImageListAdapter(Context context) {
this.context = context;
imageLoader = ImageLoader.getInstance();
}
@Override
public int getCount() {
return urls.length;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View v = convertView;
ViewHolder vh = null;
if (v == null) {
v = View.inflate(context, R.layout.single_item, null);
vh = new ViewHolder();
vh.imageView = (ImageView) v.findViewById(R.id.imageView);
v.setTag(vh);
}
else {
vh = (ViewHolder)v.getTag();
}
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisc()
.build();
imageLoader.displayImage(urls[position], vh.imageView, options);
return v;
}
private class ViewHolder {
ImageView imageView;
}
}
MainActivity.java(此处图片显示在GridView中)
public class MainActivity extends ActionBarActivity {
private ListView listView;
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
// listView = (ListView)findViewById(R.id.gridView);
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new ImageListAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
i.putExtra("id", position);
startActivity(i);
}
});
}
}
FullImageActivity.java
public class FullImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image);
Intent i = getIntent();
int position = i.getExtras().getInt("id");
ImageListAdapter imageAdapter = new ImageListAdapter(this);
ImageView imageView = (ImageView)
findViewById(R.id.full_image_view);
imageView.setImageResource(Integer.parseInt(imageAdapter.urls[position]));
}
}
当我运行android studio时错误没有显示。请帮忙。
答案 0 :(得分:0)
您是否已将新活动添加到清单文件中?
添加此
<activity
android:name=".FullImageActivity "
android:label="FullImageActivity " />
答案 1 :(得分:0)
您不需要使用getextras()
重新修改此行
Intent i = getIntent();
int position = i.getExtras().getInt("id");
到
Intent i = getIntent();
int position = i.getIntExtra("id",-1);
编辑: 您正在尝试从互联网上加载图片,您无法直接使用图片资源。
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
InputStream iS = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(iS);
image.setImageBitmap(bmp);
答案 2 :(得分:0)
你的问题是,
您的FullImageActivity
活动找不到imageView的图片资源
从这一行开始,
imageView.setImageResource(Integer.parseInt(imageAdapter.urls[position]));
您正在尝试在整数中转换字符串图片网址,因此可能是您面临的数字格式例外。
只需从给定的图片网址加载位图,然后尝试将位图设置为ImageView ..
仅用于理解的伪代码:
Bitmap imageBitmap = loadBitmapFormUrl(imageAdapter.urls[position]); // Your method which load image bitmap from given url (please use asynchronously)
imageView.setImageBitmap(imageBitmap); // After receiving image bitmap set it to imageView