我有這種循環方法來顯示ViewPager
,我爲ViewPager
上的每個圖像設置一個OnClickListener
。所以點擊圖片時,用戶將被帶到VideoPlayer類。但我不知道爲什麼,點擊偵聽器的footage_video_url[i]
上putExtra()
總是讀取額外的i
是最大長度。所以i
總是超過最大陣列長度,並使應用程序崩潰。一個循環OnClickListener總是得到錯誤的索引數組
我期待的是,如果例如顯示的圖像是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);
}
令人驚歎!工作喜歡魅力,謝謝! – hectichavana