标签: kotlin-interop

Kotlin Unresolved参考:CLI上的gradle println

println在kotlin函数之前放置一个语句会返回崩溃.堆栈跟踪:

thufir@dur:~/NetBeansProjects/kotlin$ 
thufir@dur:~/NetBeansProjects/kotlin$ gradle clean build --stacktrace
w: Classpath entry points to a non-existent location: 
e: /home/thufir/NetBeansProjects/kotlin/src/main/kotlin/example.kt: (14, 5): Unresolved reference: println

> Task :compileKotlin
Using Kotlin incremental compilation


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileKotlin'.
> Compilation error. See log for more details

* Try:
Run with --info or --debug option to get more log output.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':compileKotlin'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
        at …
Run Code Online (Sandbox Code Playgroud)

java jvm gradle kotlin kotlin-interop

5
推荐指数
1
解决办法
2893
查看次数

Kotlin文件无法使用R.java

我在相应的资源文件夹中创建了一个新的布局文件.同步后,当我尝试引用布局文件,即R.layout.activity_test时,R是一个"未解析的符号".当我尝试在AS中构建时,它失败了.

有趣的是,如果我手动导入R文件并在代码中使用它,当我尝试在命令行上构建时,它可以工作.另一件事是当我尝试使用Java文件中的R访问布局文件时,这也有效.所以我知道R.java正在生成.

我尝试创建一个全新的项目,R可以在Kotlin文件中访问.

这里有什么问题?

我尝试过的事情:

  • 清理然后建成

  • 无效的缓存/重新启动

  • 吹掉.gradle文件夹并重新创建索引

其他信息:

  • AS v3.0.1

  • 试过Kotlin v1.1.2-4和v1.1.60

  • Gradle v4.2.1

  • Gradle Plugin v3.0.0

android kotlin android-studio build.gradle kotlin-interop

5
推荐指数
1
解决办法
1134
查看次数

Kotlin中的数据类

有什么区别:

定义1

data class Person (var name:String, var age:Int)
Run Code Online (Sandbox Code Playgroud)

定义2

class Person (var name:String, var age:Int)
Run Code Online (Sandbox Code Playgroud)

定义3

class Person (){
    var name:String = ""
    var age:Int = 1
}
Run Code Online (Sandbox Code Playgroud)

在3种情况下,当我使用自动完成时,我看到像POJO一样的可用方法...是相同但3种不同的方式?

在此输入图像描述

kotlin kotlin-interop

4
推荐指数
1
解决办法
1318
查看次数

kotlin:2 lambda参数的语法

我是kotlin的新手.我有一个带有2个重载方法的java类.一个接受一个功能,另一个接受两个功能

mapToEntry(Function<? super T, ? extends V> valueMapper)
Run Code Online (Sandbox Code Playgroud)

mapToEntry(Function<? super T, ? extends K> keyMapper, 
           Function<? super T, ? extends V> valueMapper)
Run Code Online (Sandbox Code Playgroud)

nowm in kotlin,我试图调用带有2个参数的版本(如java中所示):

myClass.mapToEntry(r -> r, r -> r)
Run Code Online (Sandbox Code Playgroud)

但我得到编译错误.

Kotlin:意外的令牌(使用';'来分隔同一行上的表达式)

什么是正确的语法?

java syntax lambda kotlin kotlin-interop

4
推荐指数
2
解决办法
2083
查看次数

在Kotlin的Runnable SAM内无法获得回报?

我对这段代码有以下错误,这对我没有意义:

fun spawnWorker(): Runnable {
    return Runnable {
        LOG.info("I am a potato!")
        return
    }
}
Run Code Online (Sandbox Code Playgroud)

我的IDE对我说:

在此输入图像描述

但是Runnable接口说不然:

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}
Run Code Online (Sandbox Code Playgroud)

我之所以无法返回的原因是什么,但没有任何回报它编译得很好:

fun spawnWorker(): Runnable {
    return Runnable {
        LOG.info("I am a potato!")
    }
}
Run Code Online (Sandbox Code Playgroud)

kotlin kotlin-interop

3
推荐指数
1
解决办法
961
查看次数

用RxKotlin无法'observeOn'主线程

我正在尝试使用以下方法在主线程上观察到可观察到的结果:

    // Kotlin Code
    Observable
      .observeOn(AndroidSchedulers.mainThread())
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

    Type Mismatch:
      Required: rx.Scheduler!
      Found: io.reactivex.Scheduler!
Run Code Online (Sandbox Code Playgroud)

我要订阅的Observable来自以Java编写的库,因此使用RxJava。

我是愚蠢的东西吗?我很困惑:$

提前致谢 :)

android kotlin rx-kotlin kotlin-interop rx-java2

3
推荐指数
3
解决办法
2292
查看次数

覆盖默认构造函数中定义的变量的setter

所以我有一个看起来像这样的Kotlin类:

class MyClass {
    var myString: String = ""
        set(value) {
            field = value
            doSomethingINeed()
        }

    constructor(myString: String) {
        this.myString = myString
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,Android Studio警告我可以将其用作默认构造函数.当我选择它时,它会将其更改为:

class MyClass(var myString: String)
Run Code Online (Sandbox Code Playgroud)

现在我失去了覆盖setter的机会,因为如果我创建一个名为setMyString()I 的方法,我将收到编译器错误.

如果字段是默认构造函数的一部分,有没有办法覆盖setter,或者我是否必须使用选项1并忽略我得到的警告?

kotlin kotlin-interop

3
推荐指数
1
解决办法
1006
查看次数

Kotlin项目中使用的旧Java库中的空安全性

假设我在旧/遗留Java库中有特定代码:

public class JavaClass {
    private String notNullString;
    private String nullableString;
    private String unannotatedString;

    public JavaClass(@NotNull String notNullString,
                     @Nullable String nullableString,
                     String unannotatedString) {

        this.notNullString = notNullString;
        this.nullableString = nullableString;
        this.unannotatedString = unannotatedString;
    }

    @NotNull
    public String getNotNullString() {
        return notNullString;
    }

    @Nullable
    public String getNullableString() {
        return nullableString;
    }

    public String getUnannotatedString() {
        return unannotatedString;
    }
}
Run Code Online (Sandbox Code Playgroud)

前两个参数使用@NotNull和@Nullable注释正确注释(使用jetbrains.annotations).第三个(unnanotatedString)没有正确的注释.

当我在我的Kotlin代码中使用这个类并将所有构造函数参数设置为非null值时,一切都很好:

val foo = JavaClass("first string", "second string", "third string")

println("Value1: ${foo.notNullString.length}")
println("Value2: ${foo.nullableString?.length}")
println("Value3: ${foo.unannotatedString.length}")
Run Code Online (Sandbox Code Playgroud)

第一个值是非null,所以我可以在没有安全调用的情况下访问它.第二个值我需要使用安全调用(nullableString?.length),如果没有,我有一个编译时错误,到目前为止一直很好.在第三个值(unannotatedString)我可以在没有安全调用的情况下使用它,它编译得很好.

但是当我将第三个参数设置为"null"时,我没有得到编译时错误(不需要安全调用,只有运行时NullPointerException:

val …
Run Code Online (Sandbox Code Playgroud)

java annotations kotlin kotlin-interop kotlin-null-safety

3
推荐指数
2
解决办法
625
查看次数

Kotlin Dagger 2 Android ViewModel注入错误

我试图在我的Android应用程序上使用dagger 2从arch android库注入新的ViewModel。

从我在此示例上看到的https://github.com/googlesamples/android-architecture-components/tree/e33782ba54ebe87f7e21e03542230695bc893818/GithubBrowserSample我需要使用以下代码:

@MustBeDocumented
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)

@Module
abstract class ViewModelModule {
    @Binds
    @IntoMap
    @ViewModelKey(LoginViewModel::class)
    internal abstract fun bindLoginViewModel(viewModel: LoginViewModel): LoginViewModel

    @Binds
    @IntoMap
    @ViewModelKey(MainMenuViewModel::class)
    internal abstract fun bindSearchViewModel(viewModel: MainMenuViewModel): MainMenuViewModel

    @Binds
    internal abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
}

@ApplicationScope
@Component(modules = arrayOf(ApplicationModule::class, NetworkModule::class, ViewModelModule::class))
interface ApplicationComponent {
    fun plusActivityComponent(activityModule: ActivityModule): ActivityComponent
    fun inject(application: LISAApplication)

}
Run Code Online (Sandbox Code Playgroud)

我的工厂是:

@ApplicationScope
class ViewModelFactory @Inject constructor(private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>) …
Run Code Online (Sandbox Code Playgroud)

android viewmodel kotlin dagger-2 kotlin-interop

3
推荐指数
1
解决办法
2831
查看次数

如何在Kotlin中声明一个可以是字符串或函数的函数参数?

在下面的函数中,我想传递一个html标记的属性.这些属性可以是strings(test("id", "123"))或functions(test("onclick", {_ -> window.alert("Hi!")})):

fun test(attr:String, value:dynamic):Unit {...}
Run Code Online (Sandbox Code Playgroud)

我试图将参数声明valueAnyKotlin中的根类型.但功能不是类型Any.将类型声明为dynamic有效,但是

  • dynamic不是一种类型.它只是关闭键入检查参数.
  • dynamic 仅适用于kotlin-js(Javascript).

如何在Kotlin(Java)中编写此函数?函数类型如何与Any相关?是否有包含两种函数类型的类型Any

types dynamic kotlin kotlin-interop

3
推荐指数
1
解决办法
82
查看次数

如何在 Kotlin 中运行 Clojure?

是否可以在 kotlin 中运行 clojure?春季更具体?

我在 clojure 中制作了刮刀,我想在用 kotlin 编写的 Web 应用程序中使用它们。这在 kotlin 中看起来如何?编码..

clojure clojure-java-interop kotlin kotlin-interop

3
推荐指数
1
解决办法
806
查看次数

将 vararg 参数传递给 Kotlin 中的另一个函数时出现编译时错误

我正在尝试接受 vararg 参数作为 Kotlin 中的函数参数,并尝试将其传递给另一个带有 vararg 参数的函数。但是,这样做会给我带来编译时错误type mismatch: inferred type is IntArray but Int was expected

科特林:

fun a(vararg a: Int){
   b(a) // type mismatch inferred type is IntArray but Int was expected
}

fun b(vararg b: Int){

}
Run Code Online (Sandbox Code Playgroud)

但是,如果我在 Java 中尝试相同的代码,它就会起作用。

爪哇:

void a(int... a) {
    b(a); // works completely fine
}

void b(int... b) {

}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

java android kotlin kotlin-interop

3
推荐指数
1
解决办法
866
查看次数

将Java Option调用迁移到kotlin

我正在用kotlin迈出第一步.

我正在将一些现有的Java代码迁移到kotlin.

我有以下几点:

storyDate.ifPresent(article::setPublishDate);
Run Code Online (Sandbox Code Playgroud)

其中storyDate是一个Optional并且文章有一个方法setPublishDate(Date)方法.

我如何将此行迁移到kotlin?

https://try.kotlinlang.org上的自动迁移器是

storyDate.ifPresent(Consumer<Date>({ article.setPublishDate() }))
Run Code Online (Sandbox Code Playgroud)

但是这行不能用kotlin编译器编译.

kotlin kotlin-interop

0
推荐指数
3
解决办法
571
查看次数