Tag: 安卓

做'正常'Kotlin和Kotlin为Android不同?

在“普通”模块(在Intellij Idea中),有一个String类型的构造函数接受ByteArray , offset和length作为参数,但是当我将代码移动到“android”模块时,我发现没有这样的构造函数。 Kotlin for Android与“正常”的不同? 如何从一个字节数组创建一个String对象?

Android Kotlin:错误未解决的参考:DaggerAppComponent

我今天已经把Kotlin插件安装到一个现有的Dagger 2项目中。在Kotlin安装之前,我对Dagger没有任何问题。 但是,现在编译器抱怨: Error:(5, 32) Unresolved reference: DaggerAppComponent Error:Execution failed for task ':app:compileDebugKotlinAfterJava'. > Compilation error. See log for more details Error:(12, 21) Unresolved reference: DaggerAppComponent 项目gradle: ext.kotlin_version = '1.1.1' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" 模块gradle: kapt { generateStubs = true } dependencies { compile fileTree(dir: 'libs', include: […]

无法在Kotlin上设置ViewModel

Kotlin语言的新手,尝试Android 架构组件 。 试图在Kotlin语言中为我的LifecycleActivity设置一个ViewModel : class FooActivity : LifecycleActivity() { private var mViewModel: FooViewModel? = null .. override fun onCreate(savedInstanceState: Bundle?) { mViewModel = ViewModelProviders.of(this).get(FooViewModel.class) <– error here 取得预期的名称: 期待) 我错过了什么?

Android:与Butterknife的Kotlin

我正在尝试将Kotlin与Butterknife一起用于我的Android应用程序。 这是我的build.gradle dependencies { … compile 'com.jakewharton:butterknife:8.0.1' kapt 'com.jakewharton:butterknife-compiler:8.0.1' } kapt { generateStubs = true } 我也有一个EditText,我想用ButterKnife在更改时显示一条消息: @OnTextChanged(R.id.input) fun test() { toast(1) } 但是,没有任何反应。 我把一个断点放入函数中 – 甚至没有执行。 PS:我听说过kotterknife,但是我已经看到了纯粹的Butterknife的例子 。 我究竟做错了什么?

如何使用Kotlin设置OnEditorActionListener

所以我有这个Java代码: editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { doSomething(); return true; } return false; } }); 我已经设法得到这(我甚至不知道这是正确的方式): editText.setOnEditorActionListener() { v, actionId, event -> if(actionId == EditorInfo.IME_ACTION_DONE){ doSomething() } else { } } 但我得到一个错误Error:(26, 8) Type mismatch: inferred type is kotlin.Unit but kotlin.Boolean was expected […]

事件与kotlin和butterknife处理

将java更改为kotlin代码后,ButterKnife单击注释不起作用。 @OnClick(R.id.btnlogin) internal fun onLogin() { if (isValid) { getLoginAPi() } }

Kotlin:可空的财产代表可观察

在Kotlin中,我们可以定义一个非null属性的observable, var name: String by Delegates.observable("<no name>") { prop, old, new -> println("$old -> $new") } 然而这是不可能的 var name: String? by Delegates.observable("<no name>") { prop, old, new -> println("$old -> $new") } 定义可空属性的可观察值的方法是什么? 编辑:这是编译错误 Property delegate must have a 'setValue(DataEntryRepositoryImpl, KProperty<*>, String?)' method. None of the following functions is suitable: public abstract operator fun setValue(thisRef: […]

用Kotlin 1.1.2-4编译时的循环依赖关系,而不是1.1.2-3

我得到了下面的错误 * What went wrong: Circular dependency between the following tasks: :app:compileProductionDebugKotlin \— :app:kaptProductionDebugKotlin \— :app:compileProductionDebugKotlin (*) (*) – details omitted (listed previously) 当我使用Kotlin版本1.1.2-4(好的时候我用1.1.2-3)。 在我的应用程序build.gradle我有以下 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' 当我删除kotlin-kapt ,它编译得很好。 我做错了什么,或者这是一个Kotlin错误?

Android库,Kotlin和Dagger2

我正在构建一个包含两个模块的应用程序:核心模块,即Android库(com.android.library)和应用程序模块(com.android.application)。 将Java文件转换为Kotlin后,该项目不编译,给我一个错误,没有找到生成的Dagger 2文件(未解决的参考)。 但是那些目前正在生成的文件是: …核心\编译\生成的\源\ kapt \ {释放我的\核心\命名空间} \ DaggerBaseComponent.java 我错过了什么? build.gradle(核心模块) apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android-extensions' … android { … sourceSets { main.java.srcDirs += 'src/main/kotlin' } } dependencies { … // Dagger. kapt "com.google.dagger:dagger-compiler:2.10" compile 'com.google.dagger:dagger:2.10' provided 'javax.annotation:jsr250-api:1.0' // Kotlin compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } build.gradle(应用程序模块) apply plugin: 'com.android.application' […]

IntentService(kotlin)的默认构造函数

我是新的Kotlin和intentService一点点堆栈。 清单显示我的错误,我的服务不包含默认的构造函数,但内部服务看起来不错,没有错误。 这是我的意图服务: class MyService : IntentService { constructor(name:String?) : super(name) { } override fun onCreate() { super.onCreate() } override fun onHandleIntent(intent: Intent?) { } } 我也尝试了另一个变种: class MyService(name: String?) : IntentService(name) { 但是当我尝试运行这个服务时,我仍然得到一个错误: java.lang.Class<com.test.test.MyService> has no zero argument constructor 任何想法如何修复Kotlin中的默认构造函数? 谢谢!