Tag: kotlin

Android数据绑定不适用于Kotlin的class级授权

我的ViewModel: class MyVM( app: Application, private val observableImpl: BaseObservable, /* other colaborators*/ ) : AndroidViewModel(app), Observable by observableImpl { var currencyCode: String by Delegates.observable(“”) { _, _, newValue -> /* business logic methods */ notifyPropertyChanged(BR.currencyCode) } @Bindable get } 我的布局: 当currencyCode在数据模型中更新时, TextView不会自动更改文本。 我试图摆脱从AndroidViewModelinheritance和定义MyVM ,如下所示: class MyVM(/* other colaborators*/) : BaseObservable() { } 它的工作。 问题是,我不能丢弃我的生产代码中的AndroidViewModel的inheritance。 尽管有一个解决方法: […]

Live模板不能在Kotlin中工作

希望你已经使用Android默认的实时模板。 喜欢: Generate debug log statement: “logd”+TAB Generate error log statement: “loge”+TAB Generate info log statement: “logi”+TAB Generate TAG declaration: “logt”+TAB Generate parameter logging: “logm”+TAB Generate method return log: “logr”+TAB 它在KOTLIN中不可用? 在Android Studio 3.0 Canary版本中不可用吗?

如何在Kotlin的重载操作符上指定generics?

我正在寻找超载除法运算符“/”,但希望允许指定genericstypes来指导返回值的types。 这是一个例子: inline operator fun MyType.div(fieldName: String): T { val value = someFunction(fieldName) return convertToExpectedValue(value) } 不幸的是,我无法想出一个方法来明确指定genericstypesT,当它不能被推断。 我想知道是否有像以下这样的工作? val stringVal = myType / “myField” / “myStringField” 请注意,我知道这一点 myType.div(“blah”) 作品。 我正在寻找使用运算符“/”的东西。

如何使用Kotlin协程等待()在主线程上

我刚开始学习Kotlin协同程序,并试图模拟一些长时间的API调用,并在UI上显示结果: class MainActivity : AppCompatActivity() { fun log(msg: String) = println(“[${Thread.currentThread().name}] $msg”) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) this.setContentView(R.layout.activity_main) val resultTV = findViewById(R.id.text) as TextView val a = async(CommonPool) { delay(1_000L) 6 } val b = async(CommonPool) { delay(1_000L) 7 } launch() { val aVal = a.await() val bVal = b.await() resultTV.setText((aVal * bVal).toString()) } […]

Kotlin Gradle问题

我创建了一个用Kotlin语言编写的项目的Jar。 Jar包含以下文件夹: com jet kotlin meta-inf okio org 然后我创建了一个android项目,并添加Kotlin和Jar作为依赖项。 当我尝试执行hello world app时,会引发以下exception。 com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘/usr/lib/jvm/java-7-openjdk-amd64/bin/java” finished with non-zero exit value 1 我的应用程序构建gradle文件是 apply plugin: ‘com.android.application’ apply plugin: ‘kotlin-android’ android { compileSdkVersion 22 buildToolsVersion “22.0.1” defaultConfig { applicationId “abc.xyz.kotlin” minSdkVersion 15 targetSdkVersion 22 versionCode 1 versionName “1.0” } buildTypes { release { minifyEnabled false […]

Android Studio在layout.xml中显示错误

我正在使用数据绑定库,在Android Studio 3.0中遇到以下问题: somelayout.xml: … 在这个地方: android:text=”@{meeting.title}” Android Studio 3.0强调@符号并通知有关错误: Error:(29, 27) or expected, got ‘@’ 模型类是用Kotlin编写的,如果它有任何相关性的话。 代码完成也不适用于绑定类。 应用程序可以使用gradle命令和Android Studio运行应用程序按钮来成功构建。 所以这只是编辑问题 。 我在Mac上观察这个问题。 在Windows上,它工作正常。 我没有更多的电脑来检查是否与平台相关的问题。

Android Studio + Spek集成

我正在尝试将Spek测试框架添加到我的Android Studio项目中。 按照说明在这里 ,我结束了以下添加到我的模块build.gradle : testCompile ‘org.jetbrains.spek:spek-api:1.1.5’ testCompile ‘junit:junit:4.12’ testCompile “org.junit.platform:junit-platform-runner:1.0.0” testRuntimeOnly ‘org.jetbrains.spek:spek-junit-platform-engine:1.1.5’ 然后我用@RunWith(JUnitPlatform::class)注释了我的测试。 但是,当我尝试运行测试,我得到: org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath 任何想法我错过了什么?

Kotlin有一个标准的方式来将一个数字格式化为一个英文序数?

在Swift中,我可以这样做: let ordinalFormatter = NumberFormatter() ordinalFormatter.numberStyle = .ordinal print(ordinalFormatter.string(from: NSNumber(value: 3))) // 3rd 但是我没有办法在Kotlin那么容易地做到这一点。 有没有这样的方式,或者我将不得不使用第三方库或写我自己的?

Gradle上的Android应用程序:无法合并dex

我在Android Studio中遇到了一个错误: Execution failed for task ‘:app:transformDexArchiveWithExternalLibsDexMergerForDebug’. > com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex 我使用Gradle和Kotlin。 Android Studio 3.0.1. Windows 10 Pro 64. 我有研究这个主题,所以我尝试了一些解决方案,从这里 : 清理和重建项目。 没有变化。 将gradle中的“编译”更改为“实现”。 没有变化。 删除.gradle和build目录。 没有变化。 在gradle的defaultConfig中添加multiDexEnabled true 。 在这种情况下,我有另一个错误: Execution failed for task ‘:app:transformClassesWithMultidexlistForDebug’. > java.io.IOException: Can’t write [G:\work\myapp\app\build\intermediates\multi-dex\debug\componentClasses.jar] (Can’t read [C:\Users\Public\.gradle\caches\transforms-1\files-1.1\support-core-utils-27.0.2.aar\e1c9881d763269b67bf59dc03d19c305\jars\classes.jar(;;;;;;**.class)] (Duplicate zip entry [classes.jar:android/support/v4/content/PermissionChecker$PermissionResult.class])) 我的build.gradle : apply plugin: ‘com.android.application’ […]

Android Room:查询中的每个绑定variables都必须具有匹配的方法

我用kotlin使用android持久库库房。 Dao看起来像这样 @Dao interface CountryDao { @Query(“SELECT * FROM countries”) fun loadAllCountried() : LiveData<List> @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertAll(products: List) @Query(“SELECT * FROM countries WHERE id = :countryId”) fun loadCountry(countryId: Int): LiveData @Query(“SELECT * FROM countries WHERE id = :countryId”) fun loadCountrySync(countryId: Int): CountryEntity } 这似乎对我很好,但我得到这个错误 Error: Each bind variable in the query must have […]