我的应用程序具有一个 InfiniteScrollAdapter
,通过 URLImage
和 URLImage.ImageAdapter
填充图像。
在模拟器(Iphone3GS、Xoom 或 GoogleNexus7)中,第一次出现 InfiniteScrollAdapter
时会显示 NPE,尽管该文件确实存在于服务器上。
请注意:在此测试中,数据库中只有一个条目。因此,在下图中,您应该看到同一行(图像 + 文本)重复了 3 次。
请注意,未显示的图标中的顺序可能不同
我用来下载图像的代码是:
Image tempPlaceholder = Image.createImage(
ParametresGeneraux.SIZE_OF_REPORT_PIC_IN_PX,
ParametresGeneraux.SIZE_OF_REPORT_PIC_IN_PX,
ParametresGeneraux.accentColor);
Graphics gr = tempPlaceholder.getGraphics();
gr.setAntiAliased(true);
gr.setColor(ParametresGeneraux.accentColor);
gr.fillArc(0, 0, ParametresGeneraux.SIZE_OF_REPORT_PIC_IN_PX, ParametresGeneraux.SIZE_OF_REPORT_PIC_IN_PX, 0, 360);
EncodedImage roundPlaceholder = EncodedImage.createFromImage(tempPlaceholder, true);
final Image reportImage = URLImage.createToStorage(
roundPlaceholder,
photoFilenameInStorage,
currentReport.getPhotoPath(),
ParametresGeneraux.RESIZE_SCALE_WITH_ROUND_MASK
);
这是重写的 imageAdapter 方法:
public final static URLImage.ImageAdapter RESIZE_SCALE_WITH_ROUND_MASK = new URLImage.ImageAdapter() {
@Override
public EncodedImage adaptImage(EncodedImage downloadedImage, EncodedImage placeholderImage) {
final Image[] tmp = new Image[1];
if (!Display.getInstance().isEdt()) {
// The image scaling has to be called from EDT
Display.getInstance().callSeriallyAndWait(() -> {
tmp[0] = downloadedImage.scaledLargerRatio(placeholderImage.getWidth(), placeholderImage.getHeight());
if (tmp[0].getWidth() > placeholderImage.getWidth()) {
int diff = tmp[0].getWidth() - placeholderImage.getWidth();
int x = diff / 2;
tmp[0] = tmp[0].subImage(x, 0, placeholderImage.getWidth(), placeholderImage.getHeight(), true);
} else if (tmp[0].getHeight() > placeholderImage.getHeight()) {
int diff = tmp[0].getHeight() - placeholderImage.getHeight();
int y = diff / 2;
tmp[0] = tmp[0].subImage(0, y, Math.min(placeholderImage.getWidth(), tmp[0].getWidth()),
Math.min(placeholderImage.getHeight(), tmp[0].getHeight()), true);
}
});
} else {
tmp[0] = downloadedImage.scaledLargerRatio(placeholderImage.getWidth(), placeholderImage.getHeight());
if (tmp[0].getWidth() > placeholderImage.getWidth()) {
int diff = tmp[0].getWidth() - placeholderImage.getWidth();
int x = diff / 2;
tmp[0] = tmp[0].subImage(x, 0, placeholderImage.getWidth(), placeholderImage.getHeight(), true);
} else if (tmp[0].getHeight() > placeholderImage.getHeight()) {
int diff = tmp[0].getHeight() - placeholderImage.getHeight();
int y = diff / 2;
tmp[0] = tmp[0].subImage(0, y, Math.min(placeholderImage.getWidth(), tmp[0].getWidth()),
Math.min(placeholderImage.getHeight(), tmp[0].getHeight()), true);
}
}
EncodedImage[] image2Return = new EncodedImage[1];
if (!Display.getInstance().isEdt()) {
// The image scaling has to be called from EDT
Display.getInstance().callSeriallyAndWait(() -> {
Image roundMask = Image.createImage(tmp[0].getWidth(), tmp[0].getHeight(), 0xff000000);
Graphics gr = roundMask.getGraphics();
gr.setColor(0xffffff);
gr.fillArc(0, 0, tmp[0].getWidth(), tmp[0].getHeight(), 0, 360);
Object mask = roundMask.createMask();
tmp[0] = tmp[0].applyMask(mask);
image2Return[0] = EncodedImage.createFromImage(tmp[0], false);
});
} else {
Image roundMask = Image.createImage(tmp[0].getWidth(), tmp[0].getHeight(), 0xff000000);
Graphics gr = roundMask.getGraphics();
gr.setColor(0xffffff);
gr.fillArc(0, 0, tmp[0].getWidth(), tmp[0].getHeight(), 0, 360);
Object mask = roundMask.createMask();
tmp[0] = tmp[0].applyMask(mask);
image2Return[0] = EncodedImage.createFromImage(tmp[0], false);
}
return image2Return[0];
}
在堆栈跟踪中,NPE 似乎源于重写的 URLImage.ImageAdapter
:
java.lang.IllegalArgumentException: create image failed for the given image data of length: 0 at com.codename1.ui.Image.createImage(Image.java:654) at com.codename1.ui.EncodedImage.getInternal(EncodedImage.java:365) at com.codename1.ui.EncodedImage.getInternalImpl(EncodedImage.java:340) at com.codename1.ui.EncodedImage.getHeight(EncodedImage.java:522) at com.codename1.ui.Image.scaledLargerRatio(Image.java:899) at com.my.application.ParametresGeneraux$1.lambda$adaptImage$0(ParametresGeneraux.java:564) at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:95) at com.codename1.ui.Display.processSerialCalls(Display.java:1154) at com.codename1.ui.Display.edtLoopImpl(Display.java:1098) at com.codename1.ui.Display.invokeAndBlock(Display.java:1207) at com.codename1.ui.Display.invokeAndBlock(Display.java:1244) at com.codename1.ui.URLImage$DownloadCompleted.actionPerformed(URLImage.java:233) at com.codename1.ui.URLImage$4.onSucess(URLImage.java:301) at com.codename1.ui.URLImage$4.onSucess(URLImage.java:297) at com.codename1.util.CallbackDispatcher.run(CallbackDispatcher.java:53) at com.codename1.ui.Display.processSerialCalls(Display.java:1154) at com.codename1.ui.Display.edtLoopImpl(Display.java:1098) at com.codename1.ui.Display.mainEDTLoop(Display.java:999) at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120) at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176) [EDT] 0:0:0,1 - Codename One revisions: e5c43877074c18b4b5c7748d000e5cfac75ab749 2318
[EDT] 0:0:0,1 - Exception: java.lang.NullPointerException - null java.lang.NullPointerException at com.codename1.impl.javase.JavaSEPort.scale(JavaSEPort.java:3996) at com.codename1.ui.Image.scale(Image.java:1007) at com.codename1.ui.Image.scaledImpl(Image.java:953) at com.codename1.ui.Image.scaled(Image.java:918) at com.codename1.impl.javase.JavaSEPort$71.save(JavaSEPort.java:7659) at com.codename1.ui.EncodedImage.scaledEncoded(EncodedImage.java:626) at com.codename1.ui.EncodedImage.scaled(EncodedImage.java:653) at com.codename1.ui.Image.scaledLargerRatio(Image.java:904) at com.my.application.ParametresGeneraux$1.lambda$adaptImage$0(ParametresGeneraux.java:564) at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:95) at com.codename1.ui.Display.processSerialCalls(Display.java:1154) at com.codename1.ui.Display.edtLoopImpl(Display.java:1098) at com.codename1.ui.Display.invokeAndBlock(Display.java:1207) at com.codename1.ui.Display.invokeAndBlock(Display.java:1244) at com.codename1.ui.URLImage$DownloadCompleted.actionPerformed(URLImage.java:233) at com.codename1.ui.URLImage$4.onSucess(URLImage.java:301) at com.codename1.ui.URLImage$4.onSucess(URLImage.java:297) at com.codename1.util.CallbackDispatcher.run(CallbackDispatcher.java:53) at com.codename1.ui.Display.processSerialCalls(Display.java:1154) at com.codename1.ui.Display.edtLoopImpl(Display.java:1098) at com.codename1.ui.Display.mainEDTLoop(Display.java:999) at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120) at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
此外,浏览 .cn1 目录会发现 URLImage 存储文件名带有后缀“ImageURLTMP”,在没有 NPE 的情况下一切正常时不会出现。
最后,如果我稍后返回此表单,一切都会按预期进行(显示了图像,没有 NPE)。我尝试在 imageAdapter
中测试 downloadImage 是否为空,但 EncodedImage 不为空。
如何避免这种 NPE?
2017 年 3 月 1 日编辑
根据@Diamond和@Shai的回答,我相信发生NPE是因为InfiniteScrollAdapter想要用行填充屏幕,从而同时启动同一图像的下载(因为它不在缓存中)。因此,解决方案可能是防止 InfiniteScrollAdapter 循环(因此它变得有限)。我怎样才能做到这一点 ?
另请注意,没有 404 错误,网络监视器显示响应代码 200,如下所示。但是图像不应该被下载 3 次,不是吗?
最佳答案
将您的 ImageAdapter 更改为以下内容:
public static final URLImage.ImageAdapter RESIZE_SCALE_WITH_ROUND_MASK = new URLImage.ImageAdapter() {
@Override
public EncodedImage adaptImage(EncodedImage downloadedImage, EncodedImage placeholderImage) {
Image tmp = downloadedImage.scaledLargerRatio(placeholderImage.getWidth(), placeholderImage.getHeight());
if (tmp.getWidth() > placeholderImage.getWidth()) {
int diff = tmp.getWidth() - placeholderImage.getWidth();
int x = diff / 2;
tmp = tmp.subImage(x, 0, placeholderImage.getWidth(), placeholderImage.getHeight(), true);
} else if (tmp.getHeight() > placeholderImage.getHeight()) {
int diff = tmp.getHeight() - placeholderImage.getHeight();
int y = diff / 2;
tmp = tmp.subImage(0, y, Math.min(placeholderImage.getWidth(), tmp.getWidth()),
Math.min(placeholderImage.getHeight(), tmp.getHeight()), true);
}
Image roundMask = Image.createImage(tmp.getWidth(), tmp.getHeight(), 0xff000000);
Graphics gr = roundMask.getGraphics();
gr.setColor(0xffffff);
gr.fillArc(0, 0, tmp.getWidth(), tmp.getHeight(), 0, 360);
Object mask = roundMask.createMask();
tmp = tmp.applyMask(mask);
return EncodedImage.createFromImage(tmp, false);
}
@Override
public boolean isAsyncAdapter() {
return true;
}
};
无需检查EDT
。
确保您的 tempPlaceholder
图像首先应用于您的组件,并在逻辑结束时,在 callSerially()
中调用您的 URLImage
> 方法:
Image tempPlaceholder = Image.createImage(
ParametresGeneraux.SIZE_OF_REPORT_PIC_IN_PX,
ParametresGeneraux.SIZE_OF_REPORT_PIC_IN_PX,
ParametresGeneraux.accentColor);
Graphics gr = tempPlaceholder.getGraphics();
gr.setAntiAliased(true);
gr.setColor(ParametresGeneraux.accentColor);
gr.fillArc(0, 0, ParametresGeneraux.SIZE_OF_REPORT_PIC_IN_PX, ParametresGeneraux.SIZE_OF_REPORT_PIC_IN_PX, 0, 360);
myComponent.setIcon(tempPlaceholder);
...
//Then call this at the end of everything
Display.getInstance().callSerially(() -> {
EncodedImage roundPlaceholder = EncodedImage.createFromImage(tempPlaceholder, true);
final Image reportImage = URLImage.createToStorage(
roundPlaceholder,
photoFilenameInStorage,
currentReport.getPhotoPath(),
ParametresGeneraux.RESIZE_SCALE_WITH_ROUND_MASK
);
myComponent.setIcon(reportImage);
myComponent.getComponentForm().repaint();
});
编辑:
根据@Shai的回答,您可以检查当前是否正在下载相同的图像并防止拉取另一个图像。因为这通常会导致冲突:
//Declare this at the top of your class
final static private Map<String, Image> LOADED_URLS = new HashMap<>();
//Then change the URLImage image method to this
Display.getInstance().callSerially(() -> {
EncodedImage roundPlaceholder = EncodedImage.createFromImage(tempPlaceholder, true);
final Image reportImage = LOADED_URLS.containsKey(photoFilenameInStorage) ? LOADED_URLS.get(photoFilenameInStorage)
: URLImage.createToStorage(
roundPlaceholder,
photoFilenameInStorage,
currentReport.getPhotoPath(),
ParametresGeneraux.RESIZE_SCALE_WITH_ROUND_MASK
);
LOADED_URLS.put(photoFilenameInStorage, reportImage);
myComponent.setIcon(reportImage);
myComponent.getComponentForm().repaint();
});
关于java - 模拟器中 InfiniteScrollAdapter 中的 URLImage 显示 NPE (CodenameOne),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42500815/