小编Ale*_*hin的帖子

分类器没有伴随对象

我想在我的应用程序中使用 BottomNavigationView 并且我正面临着 kotlin 的这个问题(以前从未在 Java 中遇到过)我看到这条消息:分类器“listFragment”没有伴随对象,因此必须在此处初始化

这是我的代码:

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when (item.itemId) {
        R.id.listNav -> {
//the problem is here in listFragment word below
            setFragment(listFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.accountNav -> {
//the problem is here also in accountFragment word below
            setFragment(accountFragment)
            return@OnNavigationItemSelectedListener true
        }
false 
}
private fun setFragment(fragment: Fragment) {
    supportFragmentManager.beginTransaction().replace(R.id.mainFrame , fragment).commit()
}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏:)

fragment kotlin companion-object android-studio

6
推荐指数
2
解决办法
1万
查看次数

将 Jetbrains 公开库与 Ktor 一起使用并在协程内执行数据库事务是否安全?

我是 Kotlin 新手,最近开始在 Ktor 服务器上工作。要执行数据库操作,服务器需要与MySql服务器进行通信。我开始使用 JetBrains Exposed 库来编写数据库操作。

我编写了一个挂起函数来使用事务执行一段代码(使用 Exposed DSL 编写的数据库查询)。这是来自 ktor 入门指南的一篇博文。

suspend fun <T> dbQuery(block: () -> T): T = withContext(Dispatchers.IO) {
        transaction { block() }
    }
Run Code Online (Sandbox Code Playgroud)

每当我需要执行数据库查询时,我都会调用

dbQuery {
  // my queries
}
Run Code Online (Sandbox Code Playgroud)

因为 Exposed 使用线程本地事务管理器以及阻塞 JDBC 驱动程序,所以我想知道这样做是否安全?

没有关于如何使用协程实际处理 mysql 连接的好的文档。

如果这是错误的并最终导致事务锁定,那么任何有关如何解决此问题的指针都会有所帮助。

kotlin kotlin-exposed ktor kotlin-coroutines

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

在 vert.x 上使用事件总线注册编解码器

vert.x 事件总线的消息编解码器示例。

我有一个用例,我想注册我的自定义对象以将其发送到事件总线上。为此,我必须实现消息编解码器类。我想知道是否可以使用同一名称注册多个对象事件总线?如果不是,将多个自定义对象注册为编解码器的正确方法是什么。

java vert.x

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

Kotlin-“ where”通用约束的问题

ClassAClassB,还有如下所示的函数:

fun <T> doSomething(arg: T) where T: ClassA, T: ClassB {  }
Run Code Online (Sandbox Code Playgroud)

当我调用此函数作为参数传递类ClassC的对象时,它可以完美编译并按预期工作:

class ClassC: ClassA(), ClassB
Run Code Online (Sandbox Code Playgroud)

但是当我有一个类型的对象Any并执行以下操作时:

if (arg is ClassA && arg is ClassB) {
    doSomething(arg)
}
Run Code Online (Sandbox Code Playgroud)

编译器不会编译它并说

None of the following functions can be called with the arguments supplied
Run Code Online (Sandbox Code Playgroud)

我希望智能广播能够完成工作,但事实并非如此。请告诉我怎么了。提前致谢。

generics kotlin

2
推荐指数
1
解决办法
80
查看次数

无法从数组列表中删除元素

一些背景:我是Java的新手,我正在学习基础java课程.我目前正在进行该课程的最终项目,并完成了除最后一段代码之外的所有内容.出于某种原因,我有最艰难的时间从数组列表中删除元素.这是我正在处理的代码:

public static void delete(String bookID) {
    for (book eachElement : catalog) {
        if (eachElement.getBookID().equals(bookID)) {
            catalog.remove(eachElement);
            return;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

代码执行,没有运行时错误但它不会删除任何内容.另外,我知道在remove语句之前一切正常,因为我有另一种方法,使用与select bookID字符串相同的for和if语句来计算计算.

java arrays arraylist

1
推荐指数
2
解决办法
161
查看次数

将协程函数作为函数参数传递

我需要传递一个协程函数作为另一个函数的参数。例如:

private fun handleIt(here: Long, call: (hereId: Long) -> Unit) {
            call(here)
}
Run Code Online (Sandbox Code Playgroud)

然后从协程范围:

GlobalScope.launch {
                        handleIt(3) { viewModel.doThings(hereId) }
                    }
Run Code Online (Sandbox Code Playgroud)

viewModel 函数如下所示:

suspend fun doThings(hereId: Long) {
        withContext(coroutineContextProvider.io) {
            doSomething(hereId)
        }
    }
Run Code Online (Sandbox Code Playgroud)

但现在,我收到错误:“只能在协程体内调用暂停函数。有什么建议吗?

coroutine higher-order-functions kotlin

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

在使用Files.walk API的java8中,无法使用绝对路径从文件夹中读取文件

我正在尝试使用Files.walk api和java8中的相关路径来读取文件列表,而我正在获取java.nio.file.NoSuchFileException:\ src \ main \ resources \ eclipseconftemplates \ java,相同的api正在使用实际路径

获取异常。

try (Stream<Path> paths = Files.walk(Paths.get("/src/main/resources/eclipseconftemplates/java"))) {
            paths
                .filter(Files::isRegularFile)
                .forEach(System.out::println);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
Run Code Online (Sandbox Code Playgroud)

工作正常:

try (Stream<Path> paths = Files.walk(Paths.get("C:\\Users\\XXXX\\maven_project_demo\\antToMaven\\src\\main\\resources\\eclipseconftemplates\\java"))) {
            paths
                .filter(Files::isRegularFile)
                .forEach(System.out::println);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助。

java windows

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

在 Vert.x 最佳实践中处理数百条路线

请看下面的代码片段。现在假设我将有数百个像“人”这样的实体。您将如何编写这样的代码以使其干净、简洁、高效、结构良好?发送

class HttpEntryPoint : CoroutineVerticle() {

    private suspend fun person(r: RoutingContext) {
        val res = vertx.eventBus().requestAwait<String>("/person/:id", "1").body()
        r.response().end(res)
    }

    override suspend fun start() {
        val router = Router.router(vertx)
        router.get("/person/:id").coroutineHandler { ctx -> person(ctx) }
        vertx.createHttpServer()
            .requestHandler(router)
            .listenAwait(config.getInteger("http.port", 8080))
    }

    fun Route.coroutineHandler(fn: suspend (RoutingContext) -> Unit) {
        handler { ctx ->
            launch(ctx.vertx().dispatcher()) {
                try {
                    fn(ctx)
                } catch (e: Exception) {
                    e.printStackTrace()
                    ctx.fail(e)
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

coroutine kotlin vert.x

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

以 Kotlin 方式将 MutableList 中的所有元素增加 1

我有一个清单

val result = MutableList(N) { 0 }
Run Code Online (Sandbox Code Playgroud)

我想为列表中的元素增加 1 result

这有效

for (item in result.withIndex()) {
                result[item.index] = result[item.index] + 1
}
Run Code Online (Sandbox Code Playgroud)

但是有更多的 kotlin 方法吗?喜欢it-> it+=1?我得到 val 不能使用这个重新分配

kotlin

0
推荐指数
1
解决办法
104
查看次数