编译kotlin类时出错扩展java类

时间:2018-02-01 22:55:17

标签: java android kotlin

我有下一个java类:

public interface Callbacks {
     void onImagePickerError(Exception e, Library.ImageSource source, int type);

    void onImagePicked(File imageFile, Library.ImageSource source, int type);

    void onCanceled(Library.ImageSource source, int type);
}

和下一个抽象类扩展接口:

public abstract class DefaultCallback implements Callbacks {

    @Override
    public void onImagePickerError(Exception e, Library.ImageSource source, int type) {
    }

    @Override
    public void onCanceled(Library.ImageSource source, int type) {
    }
}

在我的情况下,需要在一个地方扩展此接口,并在来自其他库的回调时使用它。

在我的android kotlin代码中看起来像这样:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        Library.handleActivityResult(requestCode, resultCode, data, this, callback)
    }

private val callback = object: Library.Callbacks {
    override fun onImagePicked(imageFile: File?, source: Library.ImageSource?, type: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onImagePickerError(e: Exception?, source: Library.ImageSource?, type: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onCanceled(source: Library.ImageSource?, type: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}

没什么特别的。但是在编译时我有错误:

Error:(239, 28) Object is not abstract and does not implement abstract member public abstract fun onImagesPicked(@NonNull p0: (Mutable)List<File!>, p1: Library.ImageSource!, p2: Int): Unit defined in github.library.path.Library.Callbacks
Error:(240, 9) 'onImagePicked' overrides nothing

1)为什么错误的方法名称不正确 - onImage s 选择

2)为什么不编译?

我试试这个并且它有效!

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        Library.handleActivityResult(requestCode, resultCode, data, this, object: DefaultCallback() {
            fun onImagePicked(imageFile: File?, source: Library.ImageSource?, type: Int) {
                log("emm") //not worked and method useless
            }

            override fun onImagesPicked(p0: List<File>, p1: Library.ImageSource, p2: Int) {
                photoFileUri = Uri.fromFile(p0[0])
                setUpPhoto()
                log("worked") //worked! how?
            }
        })
    }

1 个答案:

答案 0 :(得分:1)

非常明显:您的private val callback没有方法onImagesPicked(p0: List<File>)

但是这个错误可能有几个原因:

  1. Kotlin看到了另一个Callbacks界面,然后我们看到public interface Callbacks。这可能是因为
    1. 输入错字
    2. 错误导入
    3. 另一个版本的库
  2. 您上面发布的代码并非完全是最新的