小编Vai*_*gal的帖子

“无法访问主线程上的数据库,因为它可能会长时间锁定 UI”另外,我使用协程访问了房间

最初,我直接访问了一个 viewModel 函数,该函数为我正在运行的查询启动了一个 viewModel 作用域协程。那有同样的错误。

然后,我将 viewModel 函数更改为挂起函数,并从片段中的协程调用它。那也没用。

因此,我使得该函数在协程内调用,然后在 viewModel 范围内运行另一个协程。这给出了相同的结果。

我认为在片段创建期间调用它可能负载太大。所以我尝试使用按钮 onclick 侦听器调用 viewModel 函数。再次坠毁。

我在数据库检查器中运行了相同的查询,它运行良好。所以,查询也不是问题。

在下面的屏幕截图中,我包含了有关该问题的所有必要细节。只需关注突出显示的内容即可。从传递列表片段(左上角选项卡)开始。从那里,调用右上角选项卡中的 viewModel 函数。从那里开始,DAO 就在它的正下方。然后是它下面的数据类。

Android Studio 截图 - 安卓工作室截图

viewModel 函数 -

fun resetAllAccess(){
    viewModelScope.launch {
        passwordDao.resetAccessForAll()
    }
}
Run Code Online (Sandbox Code Playgroud)

DAO 功能 -

@Query("UPDATE password_data SET access = 0 WHERE access = 1")
    fun resetAccessForAll()
Run Code Online (Sandbox Code Playgroud)

数据库的数据类 -

@Entity(tableName = "password_data")
data class PasswordData(
    @PrimaryKey(autoGenerate = true) val id: Int = 0,
    @ColumnInfo(name = "service_name") var serviceName: String,
    @ColumnInfo(name = "service_password") var servicePassword: String,
    @ColumnInfo(name = …
Run Code Online (Sandbox Code Playgroud)

sqlite android kotlin android-room

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

即使列表已初始化并且传递的值不为空,“.contains”也会返回空指针异常

安卓工作室截图

堆栈跟踪图像

现在工作图片

上面的屏幕截图有问题代码,待办事项注释将引导您找到问题所在。第 34 行是故障点。我已经在不同的IDE中分别尝试了带有空列表的代码,它运行得很好。这是我测试过的代码,运行良好 -

fun main() {
    val dList = mutableListOf<String>()
    val newString = "hello"
    if (dList.contains(newString)){
        print("contains")
    }else{
        print("does not contain")
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,空列表不是问题。我还尝试复制粘贴我正在关注的教程中的代码[我目前正在学习 kotlin 中的 android studio 基础知识],但这也不起作用。我什至不知道该尝试什么了。我什至在这里搜索了错误。这是针对具有初始化问题的java。我的没有那个。为了安全起见,我再次执行了前面的步骤,看看是否遗漏了什么。没有找到任何东西。所以,我被困住了。下面给出了屏幕截图中的代码[还包括注释掉的代码] -

package com.example.android.unscramble.ui.game

import android.util.Log
import androidx.lifecycle.ViewModel

class GameViewModel : ViewModel() {
    val TAG = "GameViewModel"

    init {
        Log.d(TAG, "View Model initialised")
        getNextWord()
    }
    private var _score = 0
    private var _currentWordCount = 0
    private lateinit var _currentScrambledWord: String
    val currentScrambledWord: String get() = _currentScrambledWord
    private var …
Run Code Online (Sandbox Code Playgroud)

android kotlin

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

回收者视图不尊重列表项的边距

它应该是什么样子 -

工作室样本

看起来怎么样——

安卓截图

列表项.xml -

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/card_background"
    android:layout_marginVertical="5dp"
    android:layout_marginHorizontal="10dp"
    app:cardCornerRadius="8dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/is_app_or_web_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/card_spacing"
            android:padding="@dimen/image_padding"
            app:layout_constraintBottom_toBottomOf="@id/delete_image_view"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="@id/delete_image_view"
            tools:src="@drawable/is_website_icon_24" />

        <TextView
            android:id="@+id/service_name_text_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="@dimen/card_spacing"
            android:text="@string/service_name"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="@id/delete_image_view"
            app:layout_constraintLeft_toRightOf="@id/is_app_or_web_image_view"
            app:layout_constraintRight_toLeftOf="@id/edit_image_view"
            app:layout_constraintTop_toTopOf="@id/delete_image_view" />


        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/delete_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/card_spacing"
            android:layout_marginEnd="@dimen/card_spacing"
            android:background="@color/card_foreground"
            android:padding="@dimen/image_padding"
            android:src="@drawable/ic_baseline_delete_20"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/show_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/card_spacing"
            android:background="@color/card_foreground"
            android:padding="@dimen/image_padding"
            android:src="@drawable/ic_baseline_show_20"
            app:layout_constraintBottom_toBottomOf="@id/delete_image_view"
            app:layout_constraintRight_toLeftOf="@id/delete_image_view"
            app:layout_constraintTop_toTopOf="@id/delete_image_view" />


        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/edit_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/card_spacing"
            android:background="@color/card_foreground"
            android:padding="@dimen/image_padding"
            android:src="@drawable/ic_baseline_edit_20"
            app:layout_constraintBottom_toBottomOf="@id/delete_image_view"
            app:layout_constraintRight_toLeftOf="@id/show_image_view"
            app:layout_constraintTop_toTopOf="@id/delete_image_view" /> …
Run Code Online (Sandbox Code Playgroud)

xml android kotlin android-recyclerview

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