有没有办法编写一个扩展函数来改变对象的值?

在我的情况下,我想改变一个原始的布尔 我从来不喜欢以下types的代码: private var firstTime: Boolean = true … if (firstTime) { // do something for the first time here firstTime = false } 会很好,如果我可以有一个扩展function,如: if (firstTime.checkAndUnset()) { // do something for the first time here } 这可能吗?

用协变types替换带有lambda的SAM构造函数

我有以下的Java接口: interface Action1 { void call(T t); } interface Test { void test(Action1 action) } 以下Kotlin课程: interface A { fun go() } abstract class Main { abstract fun a(): Test fun main() { a().test(Action1 { it.go() }) a().test { it.go() } } } 现在在函数main ,第一个语句编译,但是IntelliJ给出了一个警告,即SAM构造函数可以用lambda替换。 这将导致第二个陈述。 但是,这第二个语句不能编译,因为it具有typesAny? ,而不是A 删除out修饰符使其再次编译。 为什么会发生? 这个用例是当Main的实现类需要返回函数a() Test ,其中B实现了A : class […]

没有types注释的Kotlin函数参数

在Kotlin上,当定义方法时,函数的参数需要types注释。 在我的情况下,我有一个接口的两个类。 interface Base{ fun method() } class DervA():Base{ fun override method(){ … } } class DervB():Base{ fun override method(){ … } } 而且,我希望从其他函数调用他们的方法 fun test_method(inst){ inst.method() } 但是,Kotlin编译器却抱怨:“一个值参数需要一个types注解”。 我应该为每个类定义“test_method”吗? fun test_method_for_DervA(inst:DervA){ inst.method() } fun test_method_for_DervB(inst:DervB){ inst.method() } 你有更聪明的方法吗?

如何将字符串转换为字符在Kotlin?

fun main(args: Array) { val StringCharacter = “A” val CharCharacter = StringCharacter.toChar() println(CharCharacter) } 我无法将字符串A转换为字符。 我知道StringCharacter =’A’使它成为字符,但我需要转换。 谢谢。

无法解决:com.android.support

当我第一次构建一个新的Android Studio项目时,我得到了Failed to resolve: com.android.support 。 我知道在这里有几个类似的问题,但这些解决方案似乎没有为我工作。 不知道这是否相关,但请注意,错误只是Failed to resolve: com.android.support而不是Failed to resolve: com.android.support:appcompat-v7:…或其他一些Android支持库包。 这是我认为是相关的代码位。 apply plugin: ‘com.android.application’ apply plugin: ‘kotlin-android’ apply plugin: ‘kotlin-android-extensions’ android { compileSdkVersion 26 buildToolsVersion “26.0.2” defaultConfig { applicationId “checkin.yetanotherdev.com.checkin” minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName “1.0” testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner” } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’ } […]

Android工作室3.0.1:执行失败的任务’应用程序:processDebugGoogleServices’

朋友们,我最近开始在Android上工作,如果你发现一些基本的错误,请原谅。 这是我的问题。 我试图从过去的两天解决问题,但没有取得任何成功。 最近,我已经升级我的Android工作室到3.0.1,并试图编译我的项目,但我面临以下错误 错误:执行任务’:app:processDebugGoogleServices’失败。 请通过更新google-services插件的版本来修复版本冲突(有关最新版本的信息, 请访问https://bintray.com/android/android-tools/com.google.gms.google-services/ )或将com.google.android.gms的版本更新为11.0.0。 请检查我的build.gradle apply plugin: ‘com.android.application’ apply plugin: ‘kotlin-android’ apply plugin: ‘kotlin-android-extensions’ dependencies { implementation ‘com.google.firebase:firebase-messaging:11.0.0’ compile fileTree(include: [‘*.jar’], dir: ‘libs’) testCompile ‘junit:junit:4.12’ //adding volley library //Picasso compile “org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version” compile ‘com.android.volley:volley:1.0.0’ compile ‘com.squareup.picasso:picasso:2.5.2’ compile ‘com.google.gms:google-services:3.1.2’ // compile ‘com.google.gms:google-services:3.1.1’ compile ‘com.google.android.gms:play-services:11.0.0’ compile ‘com.android.support:appcompat-v7:25.2.0’ compile ‘com.android.support:design:25.2.0’ compile ‘com.google.android.gms:play-services-ads:11.0.0’ compile ‘com.google.firebase:firebase-core:10.0.1’ compile […]

在Kotlin中调用超类的构造函数,Super不是一个expression式

我有两个类Entity和Account as abstract class Entity( var id: String? = null, var created: Date? = Date()) { constructor(entity: Entity?) : this() { fromEntity(entity) } fun fromEntity(entity: Entity?): Entity { id = entity?.id created = entity?.created return this; } } 和 data class Account( var name: String? = null, var accountFlags: Int? = null ) : Entity() […]

私人可视性修改器和子包

所以我最近开始尝试与Kotlin,我偶然发现: 如果顶级声明被标记为私有,则它在其声明的包中是私有的( 参见可见性修饰符 )。 由于包真的嵌套在Kotlin中,也就是说,包foo.bar被认为是foo的成员,如果某个包是私有的,那么它的所有子包都是可见的。 请注意,外包的成员默认情况下是不导入的,即,在包foo.bar中的文件中,我们无法访问foo的成员而不导入它们。 来自: 可见性和包装嵌套 那么让我们考虑下面的例子: File1.kt package foo private fun bar() = println(“This is bar!!!”) 和File2.kt package foo.baz import foo.bar fun main(args: Array) = bar() 从我理解的function栏()应该是可见的包在foo.baz,从而可以从main()调用。 但是,当我尝试编译上述我得到以下错误信息: 错误:Kotlin:无法访问’bar’:’foo’中的’private’ 这是一个错误或有语言的规格已经更新,文件没有? 我错过了什么吗? 提前致谢。

用于在Android中启动新Activity的多个Intent标志

我试图在Intent中添加更多的标志,以便在BroadcastReceiver中启动一个新的Activity,响应从应用程序的另一部分发送的特定意图。 我已经添加,因为我注意到了LogCat消息在我创建的意图FLAG_ACTIVITY_NEW_TASK标志,然后其他的,但我在LogCat中得到相同的错误,因为FLAG_ACTIVITY_NEW_TASK不在那里。 代码如下: public class actReceiver extends BroadcastReceiver { … public void onReceive(Context context, Intent intent) { … else if (intent.getAction().equals(“something”)) { Intent prefAct = new Intent(context, PreferencesActivity.class) prefAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) .setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(prefAct); } … } … } 而特定的LogCat错误: Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is […]

Apache PDFBox将PDF转换为图像

有人可以给我一个例子,如何使用Apache PDFBox来转换不同的图像(pdf每个页面的一个pdf)的PDF。 提前致谢