我有这个循环方法来显示ViewPager
,并为OnClickListener
上的每个图片设置ViewPager
。因此,当单击图像时,用户将被带到VideoPlayer类。但是我不知道为什么,点击监听器footage_video_url[i]
上的putExtra()
总是读取额外的i
是最大长度。因此i
总是超过最大数组长度,这会导致应用程序崩溃。
我所期待的是,如果显示的图像是footage_thumbnail_url[1]
,则额外的字符串分别是footage_video_url[1]
,依此类推。
有谁知道我哪里出错了?
for(i = 0; i < length; i++){
final String[] video_url = footage_video_url;
final String[] thumbnail_url = footage_thumbnail_url;
//System.out.println("FOOTAGE VIDEO DETAILS " +i+ ": " +video_url[i]);
FrameLayout content = new FrameLayout(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
(int) size, (int) size);
params.gravity = Gravity.CENTER;
ImageView imageView = new ImageView(this);
ImageLoader loader = new ImageLoader(this);
// Image from URL
if (footage_thumbnail_url[i].contains("http"))
loader.downloadPicture(imageView, footage_thumbnail_url,i);
else {
// Image From SDCard
File imgFile = new File(thumbnail_url[i]);
Bitmap bitmapImage = loader.decodeFile(imgFile);
Drawable drawableImage = new BitmapDrawable(bitmapImage);
imageView.setBackgroundDrawable(drawableImage);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent listIntent = new Intent(getApplicationContext(),
VideoPlayer.class);
listIntent.putExtra("video_url", footage_video_url[i]);
System.out.println("Video Number "+i+" : "+ footage_video_url[i]);
startActivity(listIntent);
}
});
}
content.addView(imageView);
View overlay = new View(this);
overlay.setBackgroundResource(R.drawable.btn_play);
params.gravity = Gravity.CENTER;
overlay.setLayoutParams(params);
overlay.invalidate();
content.addView(overlay);
pagerViews.add(content);
}
答案 0 :(得分:1)
将imageView作为长度为n的数组。然后在你的for循环之前,再添加1个for循环,你将为所有'n'imageViews设置标签,如下所示:
for(int x=0;x<length;x++)
imageView[x].setTag(x);
然后在你的循环中替换这些行:
listIntent.putExtra("video_url", footage_video_url[i]);
System.out.println("Video Number "+i+" : "+ footage_video_url[i]);
这些行:
listIntent.putExtra("video_url", footage_video_url[imageView[i].getTag(i)]);
System.out.println("Video Number "+i+" : "+ footage_video_url[imageView[i].getTag(i)]);
希望这可能会有所帮助!!
答案 1 :(得分:1)
尝试设置ImageView
字符串footage_video_url[i]
的标记,然后在OnClickListener
中检索它。通过这种方式,您可以确保将正确的字符串放入附加内容中:
imageView.setTag(footage_video_url[i]);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String theUrl = (String) v.getTag(); // I hope it's a String
Intent listIntent = new Intent(getApplicationContext(),
VideoPlayer.class);
listIntent.putExtra("video_url", theUrl);
System.out.println("Video Number "+i+" : "+ theUrl);
startActivity(listIntent);
}
});