GWT 中的两个 MultiUploader 出错
Error with two MultiUploader in GWT
我想创建 2 个 MultiUploader
元素分别上传 jpg
和 pdf
我的 Composite
class GWT
public class MyClass extends Composite{
...
@UiField(provided = true)
MultiUploader muplDefPdf;
@UiField(provided = true)
MultiUploader muplDefJpg;
...
private void setMuplDef(MultiUploader muplDef, String extention,
IUploader.OnFinishUploaderHandler onFinishUploaderHandler,
IUploader.OnCancelUploaderHandler onCancelUploaderHander) {
muplDef = new MultiUploader();
muplDef.setServletPath(muplDef.getServletPath());
muplDef.setValidExtensions(extention);
muplDef.setMaximumFiles(5);
muplDef.avoidRepeatFiles(true);
muplDef.addOnFinishUploadHandler(onFinishUploaderHandler);
muplDef.addOnCancelUploadHandler(onCancelUploaderHander);
}
public MyClass() {
setMuplDef(muplDefPdf, "pdf", onFinishUploaderHandlerPdf,
onCancelUploaderHanderPdf);
setMuplDef(muplDefJpg, "jpg", onFinishUploaderHandlerJpg,
onCancelUploaderHanderJpg);
initWidget(uiBinder.createAndBindUi(this));
}
private IUploader.OnFinishUploaderHandler onFinishUploaderHandlerJpg = new IUploader.OnFinishUploaderHandler() {
@Override
public void onFinish(IUploader uploader) {
...
}
};
private IUploader.OnCancelUploaderHandler onCancelUploaderHanderJpg = new IUploader.OnCancelUploaderHandler() {
@Override
public void onCancel(IUploader uploader) {
...
}
};
private IUploader.OnFinishUploaderHandler onFinishUploaderHandlerPdf = new IUploader.OnFinishUploaderHandler() {
@Override
public void onFinish(IUploader uploader) {
...
}
};
private IUploader.OnCancelUploaderHandler onCancelUploaderHanderPdf = new IUploader.OnCancelUploaderHandler() {
@Override
public void onCancel(IUploader uploader) {
...
}
};
...
}
我的问题是出现错误
Exception caught: UiField muplDefJpg with 'provided = true' was null
为什么我在initWidget
之前定义muplDefJpg
时出现这个错误。
如何解决我的问题?
您遇到的问题与 GWT 本身 无关,而与 Java 本身有关。当你这样做时:
private void setMuplDef(MultiUploader muplDef, ...) {
muplDef = new MultiUploader();
...您必须记住,在 Java 中,您总是在调用方法时复制参数的引用(在 here 中有更多相关信息)。就此而言,您不能将 muplDefPdf
字段作为参数传递,期望它在方法内部实例化。
为了让您的代码正常工作,您需要执行以下操作:
public MyClass() {
muplDefPdf = new MultiUploader();
setMuplDef(muplDefPdf, "pdf", onFinishUploaderHandlerPdf,
onCancelUploaderHanderPdf);
muplDefJpg = new MultiUploader();
setMuplDef(muplDefJpg, "jpg", onFinishUploaderHandlerJpg,
onCancelUploaderHanderJpg);
initWidget(uiBinder.createAndBindUi(this));
}
... 并删除 setMuplDef
方法中的 muplDef = new MultiUploader();
行。
我想创建 2 个 MultiUploader
元素分别上传 jpg
和 pdf
我的 Composite
class GWT
public class MyClass extends Composite{
...
@UiField(provided = true)
MultiUploader muplDefPdf;
@UiField(provided = true)
MultiUploader muplDefJpg;
...
private void setMuplDef(MultiUploader muplDef, String extention,
IUploader.OnFinishUploaderHandler onFinishUploaderHandler,
IUploader.OnCancelUploaderHandler onCancelUploaderHander) {
muplDef = new MultiUploader();
muplDef.setServletPath(muplDef.getServletPath());
muplDef.setValidExtensions(extention);
muplDef.setMaximumFiles(5);
muplDef.avoidRepeatFiles(true);
muplDef.addOnFinishUploadHandler(onFinishUploaderHandler);
muplDef.addOnCancelUploadHandler(onCancelUploaderHander);
}
public MyClass() {
setMuplDef(muplDefPdf, "pdf", onFinishUploaderHandlerPdf,
onCancelUploaderHanderPdf);
setMuplDef(muplDefJpg, "jpg", onFinishUploaderHandlerJpg,
onCancelUploaderHanderJpg);
initWidget(uiBinder.createAndBindUi(this));
}
private IUploader.OnFinishUploaderHandler onFinishUploaderHandlerJpg = new IUploader.OnFinishUploaderHandler() {
@Override
public void onFinish(IUploader uploader) {
...
}
};
private IUploader.OnCancelUploaderHandler onCancelUploaderHanderJpg = new IUploader.OnCancelUploaderHandler() {
@Override
public void onCancel(IUploader uploader) {
...
}
};
private IUploader.OnFinishUploaderHandler onFinishUploaderHandlerPdf = new IUploader.OnFinishUploaderHandler() {
@Override
public void onFinish(IUploader uploader) {
...
}
};
private IUploader.OnCancelUploaderHandler onCancelUploaderHanderPdf = new IUploader.OnCancelUploaderHandler() {
@Override
public void onCancel(IUploader uploader) {
...
}
};
...
}
我的问题是出现错误
Exception caught: UiField muplDefJpg with 'provided = true' was null
为什么我在initWidget
之前定义muplDefJpg
时出现这个错误。
如何解决我的问题?
您遇到的问题与 GWT 本身 无关,而与 Java 本身有关。当你这样做时:
private void setMuplDef(MultiUploader muplDef, ...) {
muplDef = new MultiUploader();
...您必须记住,在 Java 中,您总是在调用方法时复制参数的引用(在 here 中有更多相关信息)。就此而言,您不能将 muplDefPdf
字段作为参数传递,期望它在方法内部实例化。
为了让您的代码正常工作,您需要执行以下操作:
public MyClass() {
muplDefPdf = new MultiUploader();
setMuplDef(muplDefPdf, "pdf", onFinishUploaderHandlerPdf,
onCancelUploaderHanderPdf);
muplDefJpg = new MultiUploader();
setMuplDef(muplDefJpg, "jpg", onFinishUploaderHandlerJpg,
onCancelUploaderHanderJpg);
initWidget(uiBinder.createAndBindUi(this));
}
... 并删除 setMuplDef
方法中的 muplDef = new MultiUploader();
行。