我想创建2个MultiUploader元素以分别在jpg中的pdf类中分别上传CompositeGWT

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行。

10-01 05:03