小编And*_*rew的帖子

必须使用 import 加载 ES Module .eslintrc.js

我花了几个小时试图解决这个问题。我几乎阅读了所有有关此问题的帖子,但仍然没有找到解决方案。

我正在尝试部署具有“https got-library”依赖项的 firebase-function,但无论我做什么,都不起作用。我不是最擅长使用 Node-js 或 TypeScript(通常是 kotlin 前端开发),所以我不知道错误想要我做什么。

Tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": false,
    "esModuleInterop": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ],
}
Run Code Online (Sandbox Code Playgroud)

.eslintrc.js

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript",
    "google",
    "plugin:@typescript-eslint/recommended",
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: ["tsconfig.json", "tsconfig.dev.json"],
    sourceType: "module",
  },
  ignorePatterns: [
    "/lib/**/*", // Ignore built files.
  ],
  plugins: [
    "@typescript-eslint", …
Run Code Online (Sandbox Code Playgroud)

javascript import node.js firebase google-cloud-functions

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

Android分页3:可以从PagingData<T>获取itemcount吗?

我怎样才能获得我PagingData<Product>持有的当前物品数量?我找到的唯一解决方案是调用,shopListAdapter.itemCount但这总是返回 0。

我想做的是:获取我的 PagingAdapter 当前显示的项目数量,将该值提交给我的 shopViewModel,然后在另一个片段(DialogFragment)中使用该值。PagingData<Product>或者:从我的视图模型内部获取项目数量

这是我目前的方法

主片段

@AndroidEntryPoint
class ShopFragment : Fragment(R.layout.fragment_shop), ShopAdapter.OnItemClickListener {
    private val shopViewModel: ShopViewModel by navGraphViewModels(R.id.nav_shop) { defaultViewModelProviderFactory }
    private var _shopBinding: FragmentShopBinding? = null
    private val shopBinding: FragmentShopBinding get() = _shopBinding!!
    @Inject lateinit var shopListAdapter: ShopAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _shopBinding = FragmentShopBinding.inflate(inflater, container, false)
        return shopBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        collectShopList()
    }

    private …
Run Code Online (Sandbox Code Playgroud)

paging android mvvm android-paging

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

Android Room:应用程序突然崩溃(致命信号 11 (SIGSEGV)、代码 1 (SEGV_MAPERR)、故障地址 0x0)

我的问题是,我的应用程序突然崩溃,我不知道为什么。这种情况发生不规律,但我认为这与我的房间数据库有关。我尝试提供尽可能多的信息:

数据库

@Database(entities = [ProductCacheEntity::class], version = 1)
@TypeConverters(Converters::class)
abstract class ShoppingCartDatabase : RoomDatabase() {
    abstract fun shopDao(): ShoppingCartDao

    companion object {
        const val DATABASE_NAME = "shop_db"
    }
}
Run Code Online (Sandbox Code Playgroud)

DAO

@Dao
interface ShoppingCartDao {
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insert(productCacheEntity: ProductCacheEntity): Long

    @Transaction
    suspend fun upsert(productCacheEntity: ProductCacheEntity) {
        val id = insert(productCacheEntity)
        if (productExists(id)) increaseQuantityById(productCacheEntity.id)
    }

    @Query("UPDATE products SET quantity = quantity + 1 WHERE ID = :id")
    suspend fun increaseQuantityById(id: Int)

    @Query("UPDATE products SET quantity = quantity - 1 …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-room

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

Android:检测打开的键盘,onApplyWindowListener 不起作用

我试图在打开键盘时隐藏布局底部的一个特定按钮,以便为用户提供更多视图。

随着androidx.core:core-ktx:1.5.0-alpha02google的发布(最终)添加了一个调用的方法insets.isVisible(WindowInsetsCompat.Type.ime()),无论键盘是打开还是点击,它都会返回一个布尔值。

我正在使用一个基类EmailFragment,我在其中设置了函数以实现上述编写的功能。我的问题是我ViewCompat.setOnApplyWindowInsetsListener(view)从未被调用过(没有吐司等)。

我也尝试ViewCompat.setOnApplyWindowInsetsListener(view)直接在使用过的 Fragments 中进行设置,但这没有任何改变。

我的最小 API 是 21,在我的 AndroidManifest.XML 中有 android:windowSoftInputMode = adjustResize

代码

abstract class EmailFragment<out T: ViewDataBinding>(
    layout: Int,
    // ... some other stuff that is not necessary for the question
) : BaseFragment<T>(layout) {
    // ... some other stuff that is not necesarry for the question

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        hideButton(view)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: …
Run Code Online (Sandbox Code Playgroud)

android android-layout kotlin

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

Resources$NotFoundException:无法使用 NestedScrollView 和 Motionlayout 找到资源 ID #0xffffffff

NestedScrollView当与 结合使用时Motionlayout,我收到上述写入错误。这里奇怪的是,只有当其中有一些项目(或,等)并且当我尝试向上移动时,才会发生错误。NestedscrollviewConstraintlayouttextviewbuttonedittextNestedScrollView

使用 aScrollView解决了这个问题,但是滚动不起作用......

BaseXML(用于 MotionLayout)

<layout 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">

    <data>
        <variable
            name="product"
            type="com.example.app.framework.datasource.models.product.Product" />
    </data>

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutDescription="@xml/shop_item_content_scene"
        app:showPaths="true"
        tools:context=".framework.ui.view.fragments.shop.ShopItemFragment">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/shop_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Light"
            app:menu="@menu/shop_item_toolbar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/product_image"
            android:layout_width="0dp"
            android:layout_height="142dp"
            android:layout_marginStart="72dp"
            android:layout_marginTop="@dimen/standard8dpMargin"
            android:layout_marginEnd="72dp"
            android:adjustViewBounds="true"
            android:background="@color/color_white"
            android:contentDescription="@null"
            android:fitsSystemWindows="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/shop_toolbar"
            tools:src="@drawable/ic_example_logo" />

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/product_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/big16dpMargin"
            android:layout_marginBottom="12dp"
            android:ellipsize="end"
            android:fontFamily="sans-serif-medium"
            android:maxLines="2"
            android:text="@{product.name}"
            android:textAlignment="textStart"
            android:textColor="@color/color_text_dark"
            android:textSize="@dimen/textHeadlineNormal1"
            app:layout_constraintBottom_toTopOf="@id/scrollview_shop"
            app:layout_constraintEnd_toEndOf="@id/barrier"
            app:layout_constraintStart_toStartOf="@id/margin_left"
            app:layout_constraintTop_toBottomOf="@+id/product_image" …
Run Code Online (Sandbox Code Playgroud)

android android-animation kotlin android-motionlayout

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

Android:LoadStateAdapter 不在 recyclerview gridlayout 中居中

我目前的问题是,LoadStateAdapter显示加载和错误状态的 my 不在 my 内居中recyclerview,它有一个 gridlayout 作为布局管理器。我在官方 android 开发者网站上没有找到任何关于此的信息,所以我在这里问:如何将我的LoadStateAdapter内部集中在我的 Recyclerview 中?

当前的

在此处输入图片说明

分段

@AndroidEntryPoint
class ShopFragment : Fragment(R.layout.fragment_shop), ShopAdapter.OnItemClickListener {
    private val shopViewModel: ShopViewModel by viewModels()
    private val shopBinding: FragmentShopBinding by viewBinding()
    @Inject lateinit var shopListAdapter: ShopAdapter

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        bindObjects()
    }

    private fun bindObjects() {
        shopBinding.adapter = shopListAdapter.withLoadStateFooter(ShopLoadAdapter(shopListAdapter::retry))
        shopListAdapter.clickHandler(this)
    }

    override fun onDestroyView() {
        requireView().findViewById<RecyclerView>(R.id.rv_shop).adapter = null
        super.onDestroyView()
    }
}
Run Code Online (Sandbox Code Playgroud)

适配器

@FragmentScoped
class ShopLoadAdapter(private val retry: () …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-recyclerview android-paging android-paging-3

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

Androidx 数据存储测试:确保您仅为此文件创建单个数据存储实例

目前,我正在为我的原始数据存储编写一些测试。我在这里遇到的唯一问题是我无法调用特定函数,因为这样我的测试就会失败/崩溃。我发现这非常令人困惑,因为我的所有其他功能似乎都有效,除了resetDatastore

这是我的代码:

存储库

private companion object {
    private const val SHOP_FILTER_PRODUCT_DATASTORE: String = "shop_filter_product_datastore_test"
    private const val SHOP_FILTER_LIST_DATASTORE: String = "shop_filter_list_datastore_test"
    private const val SHOP_FILTER_BTN_DATASTORE: String = "shop_filter_btn_datastore_test"
}

private val testNonVolatileProductDataStore = context.createDataStore(
    fileName = SHOP_FILTER_PRODUCT_DATASTORE,
    serializer = ShopFilterProductSerializer
)

private val testNonVolatileListDataStore = context.createDataStore(
    fileName = SHOP_FILTER_LIST_DATASTORE,
    serializer = ShopFilterListSerializer
)

private val testNonVolatileBtnDataStore = context.createDataStore(
    fileName = SHOP_FILTER_BTN_DATASTORE,
    serializer = ShopFilterBtnSerializer
)

override suspend fun setValueProduct(newProduct: ShopFilterTempHolder) {
    if (newProduct.id == null || newProduct.mQuery == null) …
Run Code Online (Sandbox Code Playgroud)

android sharedpreferences

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

Dagger hilt:注释类@Singleton和提供函数@Singleton之间的区别

我的问题非常简单明了:两个注释/示例有什么区别:

例一

@Singleton
class MySingletonClass() {}

@Module
@InstallIn(FragmentComponent::class)
abstract class MyFragmentModule {
   @Provides
   fun provideMySingletonClass() = MySingletonClass()
}
Run Code Online (Sandbox Code Playgroud)

实例二

class MySingletonClass() {}

@Module
@InstallIn(FragmentComponent::class)
abstract class MyFragmentModule {

   @Singleton
   @Provides
   fun provideMySingletonClass() = MySingletonClass()
}
Run Code Online (Sandbox Code Playgroud)

我知道的唯一区别是,第二个示例给出了以下错误:

error: [Dagger/IncompatiblyScopedBindings] FragmentC scoped with @dagger.hilt.android.scopes.FragmentScoped may not reference bindings with different scopes:
Run Code Online (Sandbox Code Playgroud)

这是否意味着@Singleton示例一中的注释被简单地忽略了?

android kotlin dagger dagger-2 dagger-hilt

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

如何测试 androidx(原始)数据存储,java.lang.ExceptionInInitializerError

使用数据存储一段时间并用它编写一些业务逻辑后,我想测试使用数据存储的类/存储库。我遇到的问题是,我该怎么做?context.createDatastore(...)我尝试使用测试上下文创建一个,但我遇到了一些错误。没有关于此的官方文档,或者至少我找不到。

这是我目前的方法:

界面

interface ShopFilterDataStoreRepository {
    val shopFilterProduct: Flow<ShopFilterProduct>
    val shopFilterList: Flow<ShopFilterList>

    suspend fun setValueProduct(newProduct: ShopFilterTempHolder)
    suspend fun setValueList(newList: ShopFilterTempHolder)
}
Run Code Online (Sandbox Code Playgroud)

实现(生产中使用的默认实现)

class ShopFilterDataStoreRepositoryImpl @Inject constructor(
    @ApplicationContext private val context: Context,
) : ShopFilterDataStoreRepository {

    private companion object {
        private const val SHOP_FILTER_PRODUCT_DATASTORE: String = "shop_filter_product_datastore"
        private const val SHOP_FILTER_LIST_DATASTORE: String = "shop_filter_list_datastore"
    }

    // this causes error because test context != applicationcontext
    private val nonVolatileProductDataStore = context.createDataStore(
        fileName = SHOP_FILTER_PRODUCT_DATASTORE,
        serializer = ShopFilterProductSerializer
    )

    // this causes …
Run Code Online (Sandbox Code Playgroud)

android sharedpreferences kotlin

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

导航不再工作,错误:忽略 popBackStack 到目的地,因为在当前返回堆栈上找不到它

突然,我的应用程序中的导航突然无法正常工作(昨天确实有效)。

这是一个示例:当我从 导航userLoggedInFragment到 时userLoggedOutFragment,使用参数app:popUpTo="@id/userLoggedOutFragment" app:popUpToInclusive="true",然后单击后退按钮,即使我使用了 popUpTo,它仍然会将我导航回 userLoggedInFragment。然后我得到信息:

I/NavController: Ignoring popBackStack to destination com.example.app:id/userLoggedOutFragment as it was not found on the current back stack
Run Code Online (Sandbox Code Playgroud)

这是我的片段代码

用户登录片段

@AndroidEntryPoint
class UserLoggedInFragment : Fragment() {
    private var _binding: FragmentUserLoggedInBinding? = null
    private val binding: FragmentUserLoggedInBinding get() = _binding!!
    private val viewModel: LogoutViewModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentUserLoggedInBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-navigation android-jetpack android-architecture-navigation

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