编译kotlin类扩展java类时出错
我有下一个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, p1: Library.ImageSource!, p2: Int): Unit defined in github.library.path.Library.Callbacks Error:(240, 9) 'onImagePicked' overrides nothing
1)为什么错误有不正确的方法名称 – 图像被选中
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, p1: Library.ImageSource, p2: Int) { photoFileUri = Uri.fromFile(p0[0]) setUpPhoto() log("worked") //worked! how? } }) }
这是非常明显的:你的private val callback
没有方法onImagesPicked(p0: List
。
但是这个错误可能有几个原因:
- Kotlin看到了一些另外的
Callbacks
接口,然后我们看到了public interface Callbacks
。 这可能是因为- 输入错字
- 错误的导入
- 另一个版本的库
- 您上面发布的代码并不完全是最新的