Kotlin全开放编译器插件不起作用

我使用Realm,它需要open关键字模型类。 在https://blog.jetbrains.com/kotlin/2016/12/kotlin-1-0-6-is-here/之后,我尝试使用全开放编译器插件从Realm模型类中删除open关键字。 首先,我添加了全开放的编译器插件,并设置了注释的包名称 buildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" } } apply plugin: "kotlin-allopen" allOpen { annotation("com.mycompany.myapp.annotation") } 其次,我生成了注释 package com.mycompany.myapp.annotation annotation class AllOpenAnnotation 最后,我添加了对Realm模型类的注解 @AllOpenAnnotation class Model { var id: Int = -1, var title: String = "", var desc: String? = null }: RealmObject() 但是error: cannot inherit from final Model发生错误。 有什么我做错了吗?

未解决的参考:Kotlin中的findViewById

fun Tryouts() { var CheckBox1 : CheckBox = findViewById(R.id.ForwardBox) as CheckBox CheckBox1.setChecked(false) } 我还是Kotlin的初学者,仅仅学习了kotlin的基本工作,我无法引用任何android小部件或者在Android Studio中更改它的状态,无论是TextView还是CheckBox或者RadioBox。 在所有情况下,findViewById的相同未解决的引用错误… 我不知道我在做什么错误,甚至是java转换输出相同的错误。

不能使用Gradle和Kotlin编译项目

在从1.1.4-3到1.1.50(或51)碰撞kotlin-gradle-plugin之后,以及所有与Kotlin相关的库,在尝试导入gradle文件时遇到类似下面的错误: Unable to build Kotlin project configuration Details: java.lang.reflect.InvocationTargetException: null Caused by: java.lang.NoSuchMethodError: kotlin.reflect.jvm.internal.KClassImpl.getData()Lkotlin/reflect/jvm/internal/ReflectProperties$LazyVal; 使用较旧版本的插件(1.1.4-3)时,编译工作正常。 完整的gradle文件: apply plugin: 'war' apply plugin: 'com.google.cloud.tools.appengine' apply plugin: 'kotlin' apply plugin: 'kotlin-kapt' apply plugin: "net.ltgt.apt" apply plugin: 'idea' apply plugin: 'org.jetbrains.dokka' def daggerVersion = "2.11" sourceCompatibility = '1.8' targetCompatibility = '1.8' buildscript { repositories { mavenCentral() maven { url […]

Kotlin泛型与扩展

所以,我一直试图在Kotlin中实现一些Java,并且面临将参数传递给通配类的方法的挑战。 例如: interface A class B<in T : A> { fun pass(e: T) { /* do something */ } } class C { private val things = mutableListOf<B<*>>() fun test(e: A) { things.get(0)?.pass(e) // This doesn't work, since wildcard B generic wants Nothing, since Nothing can be safely passed to it. // but, I know […]

这是Kotlin REPL的错误吗?

我可以像这样(递归地)在Kotlin REPL中定义一个值而不会出现错误: val s: String = s 现在我得到一个's'类型的NotNull,但是null的值。 我可以用NPE来做这个NotNull值: >>> val s: String = s >>> s.length java.lang.NullPointerException 你可以尝试一下你自己的Kotlin REPL,它每次都有效。 我正在使用Kotlin版本1.1.2-3。

在Kotlin中使用BufferedReader的最佳方式

所以我刚刚开始使用Kotlin for Android(希望它可以帮助我使用Swift),并将Android Java代码转换为Kotlin。 在其中一个转换中,我偶然发现了一个BufferedReader,我通常会用Java编写(或者说C#),如下所示: String result = ""; String line = ""; BufferedReader reader = new BufferedReader(someStream); while ( (line = reader.readLine()) != null ) { result += line; } 但是在Kotlin中,似乎Kotlin不允许我在条件的情况下给变量赋值。 目前,我已经编写了如下代码: val reader = BufferedReader(someStream) var line : String? = "" while (line != null) { line = reader.readLine() result += line } […]

Kotlin反射地调用getter / setter

初学者在Kotlin这里。 我尝试通过在程序中反射来创建和填充对象。 我无法在纯kotlin中找到等价的功能,所以我的解决方案类似于下面的代码,它的工作正常,但需要使用像java.lang.String::class.java这样的dirty参考,可以理解,似乎不喜欢这个。 有没有更简单的方法,我失踪要做到这一点? val jclass = myObject::class.java val setters = jclass.declaredMethods.filter { it.name.startsWith("set") } for (s in setters) { val paramType = s.parameterTypes.first() val data = when(paramType) { java.lang.Integer::class.java -> foo java.lang.Double::class.java -> bar java.lang.String::class.java -> baz } s.invoke(myObject, data) }

在Kotlin中设置Guice绑定

我想设置Guice绑定,所以我创建了一个完美的Java模块: public class CrashLoggerModule extends AbstractModule { @Override public void configure() { bind(CrashLogger.class).to(ConcreteCrashLogger.class); } } 然后我把这个代码转换成Kotlin: public class CrashLoggerModule : AbstractModule() { override fun configure() { bind(javaClass<CrashLogger>()).to(javaClass<ConcreteCrashLogger>()) } } 不幸的是,这个类的Kotlin版本不再工作了。 发生这种情况是因为Kotlin将其内部方法称为public fun <A, B> A.to(that: B): Pair<A, B>而不是LinkedBindingBuilder<T>.to(Class<? extends T> c) Guice绑定没有正确设置。 我怎样才能明确指出,我想使用类方法,而不是扩展功能?

释放kotlin中的变量

在kotlin中,为了编写测试,我在setUp阶段的每个测试之前创建了一个guice注入器,然后尝试在tearDown阶段释放它: class MyTestClass { …. var injector: Injector? = null … @Before fun setUp() { … injector = Guice.createInjector(modules) … } @After fun tearDown() { … injector = null } @Test fun myFirstTest() { myInstance = injector?.getInstance(Key.get(MyClass::class.java)) // use myInstance here … } … } 我的问题是,在Kotlin的土地上,这样的必要性是不是必需的? 如果有必要,最好的办法是什么?

Java与Kotlin泛型

我有下面的Java类,我试图转换为使用泛型的Kotlin。 抽象类MvpViewHolder,M,V:View?>(itemView:View):RecyclerView.ViewHolder(itemView){ public abstract class MvpViewHolder<P extends BasePresenter> extends RecyclerView.ViewHolder { protected P presenter; public MvpViewHolder(View itemView) { super(itemView); } public void bindPresenter(P presenter) { this.presenter = presenter; presenter.bindView(this); } public void unbindPresenter() { presenter = null; } } 这是我的Kotlin尝试 abstract class MvpViewHolder<P : BasePresenter>(itemView: View) : RecyclerView.ViewHolder(itemView) { protected var presenter: P? = null […]