我想创建2个MultiUploader
元素以分别在jpg
中的pdf
类中分别上传Composite
和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
为什么在
muplDefJpg
之前定义initWidget
时出现此错误。如何解决我的问题?
最佳答案
您遇到的问题与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));
}
...并删除
muplDef = new MultiUploader();
方法内的setMuplDef
行。