Tag: 科特林

Android Studio Kotlin dataSnapshot.getValue错误

我已经在最近的项目中将java文件转换为kotlin文件,问题是我正在面对与此代码的错误: val map = dataSnapshot.getValue<Map>(Map::class.java) 我在“Map :: class”下有一个红线,android studio说: 只有类允许在类文字的左侧 我应该如何处理这个代码? 有没有其他的方式来写呢? 这是一个相对的kotlin代码片段: val messageText = messageArea!!.text.toString() if (messageText != “”) { val map = HashMap() map.put(“message”, messageText) map.put(“user”, UserDetails.username) reference1!!.push().setValue(map) reference2!!.push().setValue(map) messageArea!!.setText(“”) } } reference1!!.addChildEventListener(object : ChildEventListener { override fun onChildAdded(dataSnapshot: DataSnapshot, s: String) { val map = dataSnapshot.getValue<Map>(Map::class.java) val message = map.get(“message”).toString() val […]

初始化方法inheritance

如果我有一个init方法抽象类A: abstract class A(){ init { println(“Hello”) } } 然后是扩展A的B类 class B(): A() 如果我像这样实例化B. fun main(args: Array){ B() } A中的init方法仍然运行并且Hello打印出来了吗? 如果没有,我需要做些什么来让A的init方法运行?

什么? 在Kotlin中是否指派任务的左侧?

根据Kotlin文档 ,? 运算符表示“安全调用”,这意味着如果它在方法调用链中使用,则整个链将返回null(如果它使用的值为null)。 但是如果在作业的左侧使用它呢? 由于左侧不是“返回”任何一方,似乎可能有不同的效果。 这是我正在谈论的一个例子: val myObj = SomeObj() myObj?.property = SomeClass.someFunc() // What does ?. do in this context?

这个错误是什么意思,当从java代码转换到通用的kotlin,单T后禁止?

试图将一些java代码转换为kotlin,并与genericstypes的问题。 这里的一个示例是Danny Preussler的Bundles和ViewModel的post的Java代码: 这里 class BundleAwareViewModelFactory implements ViewModelProvider.Factory { private final Bundle bundle; private final ViewModelProvider.Factory provider; public BundleAwareViewModelFactory(@Nullable Bundle bundle, ViewModelProvider.Factory provider) { this.bundle = bundle; this.provider = provider; } @SuppressWarnings(“unchecked”) @Override public T create(final Class modelClass) { T viewModel = (T) provider.create(modelClass); if (bundle != null) { viewModel.readFrom(bundle); } return viewModel; } } […]

ByteArray和数组在kotlin中的区别

我不明白为什么例如Java中声明为返回byte[]的java.security.MessageDigest.digest()方法返回Kotlin中的ByteArray ,尽管Kotlin通常似乎调用byte[] Array 。 例如以下不起作用: fun main(args : Array) { val md = java.security.MessageDigest.getInstance(“SHA”) if (md == null) throw NullPointerException() val result : Array? = md.digest() } types不匹配:推断的types是ByteArray? 但Array? 预计

是否有相当于AssertJ库的Kotlin?

我正在将一些测试从Java转换到Kotlin。 对于Java测试,我使用AssertJ库,这个库非常强大,并且拥有丰富的断言。 我的问题是,对于Kotlin测试,我不能使用AssertJ和Kotlin JUnit( org.jetbrains.kotlin:kotlin-test-junit )具有非常有限的一组断言。 有没有Kotlin相当于AssertJ或更好的断言方式? 我find了Kluent库,但我仍不确定这是否是最好的库。

如何在Kotlin中添加数组索引值?

首先,我在同伴对象中创建空数组(Kotlin)实例。 companion object { var strarray: Array = arrayOf() var objectarray: LinkedHashMap<Int, List> = LinkedHashMap<Int, List>() } 我希望从CSV文件中读取textString时使用空数组实例。 fun csvFileToString():String { val inputStream = File(Paths.get(“”).toAbsolutePath().toString() .plus(“/src/main/SampleCSVFile_2kb.csv”)).inputStream() val reader = inputStream.bufferedReader() var iterator = reader.lineSequence().iterator() var index:Int = 1; while (iterator.hasNext()){ var lineText:String = iterator.next() strarray.set(index, lineText) index++ } return “” } 但是当我运行该源代码 a.csvFileToString() println(CsvParser.strarray) 发生exception […]

Kotlin为什么不执行自动types转换?

var a : Double a = Math.sin(10) // error: the integer literal does not conform to the expected type Double a = Math.sin(10.0) //This compiles successfully println(a) kotlin为什么不执行隐式types转换并强制我们传递确切types的数据? fun sin(value: Double): Double // at kotlin documentation

kotlin库模块中调试目录的未解决的参考

我第一次使用Kotlin开发Android应用程序。 目前我正在使用我的项目中的一个库模块(“数据”),与主应用程序模块分开: > [Root directory] > app > […] > data > src > debug > kotlin > com.domain.app > […] > main > kotlin > com.domain.app > […] > release > kotlin > com.domain.app > […] 我遇到了一个障碍:每当data.src.main内的文件引用data.src.debug中的文件时,构建过程将失败,并显示消息“Unresolved reference:[class name]”。 但是,当我在data.src.release中引用文件时,它不会失败。 在这两种情况下,我已经三重检查了语法,目录和构建变体。 我没有修改我的build.gradle文件中的调试版本或发布版本 – 我使用的是默认设置。 我的源文件是: android { … sourceSets { main.java.srcDirs += ‘src/main/kotlin’ debug.java.srcDirs […]

无法通过Springboot连接到远程MongoDB

我试图连接到一个远程的MongoDB。 由于我没有得到一个MongoOpenSocketException,我认为已经建立了一个连接,但需要更多的启动应用程序。 我错过了什么? Application.kt package hello import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.EnableAutoConfiguration import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration import org.springframework.context.annotation.Configuration /** * The main entry point to the application */ @EnableAutoConfiguration(exclude = arrayOf(MongoAutoConfiguration::class)) @Configuration class Application /** * Run the application * @param args The command line arguments */ fun main(args: Array) { SpringApplication.run(Application::class.java, *args) } 的build.gradle: // Top-level build file […]