Java 将参数传递给其他类时出现问题
我有一个相当奇怪的问题。我试图将一些参数传递给另一个类中的方法,四个参数中有三个传递得很好。然而,第四个是返回0(零),不管我看起来做了什么 我正在初始化第二个类ImageLoader,没有任何问题 这是有问题的电话-我添加了一些评论来解释我的问题:Java 将参数传递给其他类时出现问题,java,android,methods,parameters,android-3.0-honeycomb,Java,Android,Methods,Parameters,Android 3.0 Honeycomb,我有一个相当奇怪的问题。我试图将一些参数传递给另一个类中的方法,四个参数中有三个传递得很好。然而,第四个是返回0(零),不管我看起来做了什么 我正在初始化第二个类ImageLoader,没有任何问题 这是有问题的电话-我添加了一些评论来解释我的问题: imageLoader.DisplayImage(coverFileNames.get(position), Main.this, (ImageView) convertView, position); // coverFileNames.get(
imageLoader.DisplayImage(coverFileNames.get(position), Main.this, (ImageView) convertView, position); // coverFileNames.get(position) works great and returns the correct filename based on the position - position on its own, however, doesn't!
下面是DisplayImage方法:
public int position;
public void DisplayImage(String fileUrl, Activity activity, ImageView imageView, final int pos) {
position = pos; // this is 0 no matter what I do
imageViews.put(imageView, fileUrl);
queuePhoto(fileUrl, activity, imageView);
imageView.setImageResource(R.drawable.noposterlarge);
}
public void DisplayImage(String fileUrl, Activity activity, ImageView imageView, final int pos) {
Log.v("Testing", "position = " + pos); // This returns 0, which is not correct
imageViews.put(imageView, fileUrl);
queuePhoto(fileUrl, activity, imageView);
imageView.setImageResource(R.drawable.noposterlarge);
}
有什么想法吗?谢谢
编辑:
下面是GetView方法:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = (ImageView) new ImageView(Main.this);
}
// Create new file for the file path of the movie
File file = new File(videoUrls.get(position));
// Create variables for potential custom art check
boolean potentialImage = false;
String ImageFile = null;
String[] potentialImageFiles = new String[]{file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".")) + ".jpg",
file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".")) + ".jpeg",
file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".")) + ".JPG",
file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".")) + ".JPEG"};
// Check if each file exists and return if one does
for (String potentialFile : potentialImageFiles) {
if (!potentialImage) {
if (new File(potentialFile).exists()) {
potentialImage = true;
ImageFile = potentialFile;
}
}
}
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
options.inPreferredConfig = Config.RGB_565;
// Check if there's a custom cover art
if (potentialImage) {
Log.d("TEST", "POS: " + position); // this returns the correct value
imageLoader.DisplayImage(ImageFile, Main.this, (ImageView) convertView, position);
} else {
Log.d("TEST", "POS: " + position); // this returns the correct value
imageLoader.DisplayImage(coverFileNames.get(position), Main.this, (ImageView) convertView, position);
}
return convertView;
}
同样,这里是DisplayImage方法:
public int position;
public void DisplayImage(String fileUrl, Activity activity, ImageView imageView, final int pos) {
position = pos; // this is 0 no matter what I do
imageViews.put(imageView, fileUrl);
queuePhoto(fileUrl, activity, imageView);
imageView.setImageResource(R.drawable.noposterlarge);
}
public void DisplayImage(String fileUrl, Activity activity, ImageView imageView, final int pos) {
Log.v("Testing", "position = " + pos); // This returns 0, which is not correct
imageViews.put(imageView, fileUrl);
queuePhoto(fileUrl, activity, imageView);
imageView.setImageResource(R.drawable.noposterlarge);
}
这根本不可能 添加到显示图像的顶部
Log.v("DisplayImage", "position = " + position);
如果是
0
从更改调用代码
imageLoader.DisplayImage(coverFileNames.get(position), Main.this, (ImageView) convertView, position);
到
现在,它会显示2
它必须
这意味着在调用代码的行
imageLoader.DisplayImage()
中,由于某种原因,位置一直是0
coverFileNames.get(position)
总是在position=0处返回字符串
尝试从DisplayImage函数中删除final
修饰符
public void DisplayImage(String fileUrl, Activity activity, ImageView imageView, int pos)
为什么不告诉我们如何获取变量“position”…?它是BaseAdapter中GetView方法中的position变量。对不起,我忘了。但是它是完全有效的,我在GetView方法中使用它没有问题。为什么不发布这个类的完整代码呢…没有什么错@Michel请给我看一下调用imageLoader.DisplayImage(coverFileNames.get(position),Main.this,(ImageView)convertView,position)的代码块代码>它是一个函数吗?编辑你的问题你能在这些区域添加你的完整代码以及你对日志记录等的所有调用吗?我感觉一定有其他事情发生了,但你上面的代码片段没有显示出来。08-09 10:27:42.820:VERBOSE/DisplayImage(29966):position=0这非常奇怪,我不知道为什么会发生这种情况。将其添加到DisplayImage的“top”中显然会写入0,因为“position”尚未初始化。是的,这就是我的想法,所以我将其更改为:Log.v(“DisplayImage”,“position=“+pos”)代码>@Sherif是的,这将其更改为2。好的,我编辑了。。很明显,position始终为零,您对coverFileNames.get(position)
的理解是错误的,它不起作用