小编Kir*_*nLy的帖子

Kotlin,Android,如何正确调试协程?

我试图调试我的协程,但放置在挂起函数中的断点不起作用。请帮助我理解为什么。

使用 Android Studio。

好的,我从 viewModelScope 启动了一个协程:

    viewModelScope.launch(IO) {
        when(val result = interactor.getAllWords()){...}
    }
Run Code Online (Sandbox Code Playgroud)

getAllWords()我写道:

    override suspend fun getAllWords(): WordResult {
        val words = mutableListOf<Word>()

        when (val wordsResult = getAllWordsWithoutFiltersApplying()) {}

        ...

        return getWordsWithSelectedPattern()
Run Code Online (Sandbox Code Playgroud)

我有两个挂起功能:getAllWordsWithoutFiltersApplying()getWordsWithSelectedPattern()。我对它们都有一个断点,但它们没有在调试模式下触发。

同时,线 val words = mutableListOf<Word>(),当我将断点放在它的行上正在触发。

而且,如果我将一些日志内容放入“untracing”功能中,它们将起作用。我这么说是为了说明,暂停功能有效。断点不是。

我该怎么做才能调试它们?

*截图已添加。看左边有一排图标。为什么我的线路不可用?

在此处输入图片说明

debugging kotlin android-studio kotlin-coroutines

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

无法在测试中使用 Mockito 和 Kotlin 模拟最终课程

我正在尝试运行一个简单的仪器测试:

class DefaultTest {

    private val networkHelper = Mockito.mock(NetworkHelperImpl::class.java)

    @Test fun inter() {
        given(networkHelper.isConnectedToNetwork()).willReturn(true)
        assertFalse { networkHelper.isConnectedToNetwork(false) }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我不能因为错误:

Mockito cannot mock/spy because :
- final class
Run Code Online (Sandbox Code Playgroud)

我怎样才能避免它?

正如本指南所说:

https://antonioleiva.com/mockito-2-kotlin/

我正在创建文件:

在此处输入图片说明

有了这条线:

mock-maker-inline

但什么都没有改变。

Gradle 很糟糕(我正在学习),但它必须工作。我也使用单元测试,所以现在我有:

//Tests
testImplementation 'junit:junit:4.13-beta-3'

testImplementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.41'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit:1.3.41'

androidTestImplementation 'org.mockito:mockito-core:3.0.0'
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"

testImplementation 'org.amshove.kluent:kluent:1.14'

androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:rules:1.2.0'
Run Code Online (Sandbox Code Playgroud)

testing mockito kotlin

9
推荐指数
3
解决办法
1万
查看次数

Android Studio 中的每个预览中都会显示闪屏主题

我使用我自己的主题创建了一个 SplashScreen...

<style name="AppTheme.Launcher">
        <item name="android:windowBackground">@drawable/splash_test</item>
</style>
Run Code Online (Sandbox Code Playgroud)

...应用程序加载时应用为应用程序的主主题...

android:theme="@style/AppTheme.Launcher"
Run Code Online (Sandbox Code Playgroud)

...然后在启动的Activity的onCreate中更改为AppTheme:

setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
Run Code Online (Sandbox Code Playgroud)

一切正常,但我有一个有趣的问题。所有的屏幕预览都使用Launcher主题,现在看起来像:

在此输入图像描述

我就是不能这样工作。=)

我怎样才能避免这个“问题”呢?

android themes splash-screen

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

当我导航回片段时,片段的内容消失了

我在“单一活动”应用中使用“导航”组件进行导航,但是我有一个片段的奇怪行为。这只是为了解释使用图像。

我有一个ViewPager片段。ViewPager包含另外两个片段,因此看起来:

在此处输入图片说明

-第一个片段,测试 -第二个片段。Bottom元素是Bottom Navigation,它不是片段的一部分。片段在工具栏底部导航之间。

这个包含ViewPager的片段不是起始片段,它位于堆栈的中间。

因此,当用户单击底部菜单项时,此导航代码正在运行(来自Main Activity):

 bottom_navigation.apply {
            itemIconTintList = null
            setOnNavigationItemSelectedListener { item ->
                when (item.itemId) {
                    R.id.about_bottom -> {
                        findNavController(R.id.host).navigate(R.id.toAboutUs)
                    }
                    R.id.error_bottom -> {
                        findNavController(R.id.host).navigate(R.id.toMessage)
                    }
                }
                true
            }
        }
Run Code Online (Sandbox Code Playgroud)

其中toMessage/ toAboutUs是另一个片段的全局点。

那是什么问题。当用户单击底部菜单项时,一切正常。但是当他按下“返回”按钮时,片段中的内容就消失了。只是看到:

在此处输入图片说明

我什至无法建议原因。我知道“主要”片段和ViewPager上的片段没有重新创建,那么为什么它们会丢失内容?

我没有在任何地方覆盖后退按钮的行为。我只是使用app:defaultNavHost="true"主机片段。


数据的传输方式: 当用户单击按钮以用ViewPager打开Fragment时,将从数据库中加载数据并保存到ViewModel,然后才将用户传输到该Fragment。创建两个子片段时,它们将从ViewModel加载数据。而且我在清除ViewModel的代码中没有位置,因此当用户按回它时,我的ViewModel 100%包含某些内容。但是不显示。

UPD:花了一些时间,我意识到回溯时并没有重新创建两个“子”片段,而是重新创建了Main Fragment。我认为问题出在此,但仍然不知道确切的位置。

我需要您的帮助以了解发生了什么。


更新:提供片段创建的一些代码。BaseCompatFragment扩展Fragment

MainFragment(另一个两个Fragment的容器):

class QuestionFragment : BaseCompatFragment() {

    override fun onCreateView( …
Run Code Online (Sandbox Code Playgroud)

navigation android android-fragments android-viewpager kotlin

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

导航抽屉不适用于导航组件

当用户单击菜单项时,导航抽屉不会将用户带到目的地。抽屉式导航栏显示正确,但不起作用。有什么问题吗?

我有一个导航菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        ...        
        <item
            android:id="@+id/feedbackFragment"
            android:icon="@drawable/ic_help"
            android:title="@string/title_help" />
    </group>

</menu>
Run Code Online (Sandbox Code Playgroud)

和导航图:

  <fragment
      android:id="@+id/feedbackFragment"
      android:name="com.company.ru.ui.FeedBackFragment"
      android:label="fragmegment_feedback"
      tools:layout="@layout/fragment_feed_back" />
Run Code Online (Sandbox Code Playgroud)

在我的 MainActivity 中,我有一个工具栏(全部复制):

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:navGraph="@navigation/main_graph" />
    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/color_white"
        android:theme="@style/NavigationView"
        app:headerLayout="@layout/nav_header"
        app:itemIconPadding="@dimen/margin_16"
        app:itemIconTint="#FFD000"
        app:itemTextColor="@android:color/black"
        app:menu="@menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

我这样初始化我的抽屉:

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); …
Run Code Online (Sandbox Code Playgroud)

navigation android navigation-drawer

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

为什么 Kotlin 中的 Any 只有 3 个方法,而 Java 中的 Object 有 10 个以上?

从文档中,我知道AnyKotlin中的类是:

Kotlin 类层次结构的根。每个 Kotlin 类都有 [Any] 作为超类。

它看起来像Object在 Java 中,文档说:

类对象是类层次结构的根。每个类都有一个 Object 作为超类。所有对象,包括数组,都实现了这个类的方法。

我知道底层Any将是Object:我使用带有“反编译”选项的屏幕。所以我真的不明白,如果AnyObject最后,为什么Any只有三种方法,什么时候Object有这么多?

这样做的原因是什么?

在此处输入图片说明

在此处输入图片说明

java kotlin

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