繁体   English   中英

编译Kotlin类扩展Java类时出错

[英]Error with compile kotlin class extend java class

我有下一个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 Picked

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?
            }
        })
    }

这很明显:您的private val callback没有onImagesPicked(p0: List<File>)

但是,可能有多种原因导致此错误:

  1. Kotlin看到了另一个Callbacks接口,然后是我们看到的public interface Callbacks 可能是因为
    1. 进口错字
    2. 导入错误
    3. 另一个版本的库
  2. 您上面发布的代码不是最新的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM