简体   繁体   English

使用 Hilt 将依赖项注入 Android 类时无法运行

[英]Not able to run when injecting dependencies with Hilt into Android Classes

I'm in the process of making an application using Hilt.我正在使用 Hilt 制作应用程序。 But my question is, am I only able to run my app through AndroidManifest.xml?但我的问题是,我只能通过 AndroidManifest.xml 运行我的应用程序吗? I'd like to run it through another class, but it keeps giving me a blank page.我想通过另一个 class 运行它,但它一直给我一个空白页。

My Classes:我的课程:

Application class using @HiltAndroidApp使用@HiltAndroidApp 的应用程序 class

@HiltAndroidApp
class ExampleApplication : Application()

Activity class using @AndroidEntryPoint使用@AndroidEntryPoint 的活动 class

@AndroidEntryPoint
class ExampleActivity : ComponentActivity() {
    @RequiresApi(Build.VERSION_CODES.N)
    override fun onCreate(instance: Bundle?) {
        super.onCreate(instance)
        setContent {
            AppTheme {
                Surface() {
                 ......

Manifest.xml (This is the only way I can run the class, ExampleActivity). Manifest.xml(这是我可以运行 class,ExampleActivity 的唯一方法)。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:name=".ui.ExampleApplication"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.App"
        tools:targetApi="31">
        <activity
            android:name=".ExampleActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.App">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

So, I've tried calling my application from another class, but I can't call ExampleActivity.kt alone, so I tried calling two classes, but it keeps giving me a blank page.所以,我尝试从另一个 class 调用我的应用程序,但我不能单独调用 ExampleActivity.kt,所以我尝试调用两个类,但它一直给我一个空白页。

Here's what I've tried:这是我尝试过的:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AppTheme {
                 ExampleApplication()
                 ExampleActivity()
            }
        }
    }
}

It gives a blank screen.它给出了一个空白屏幕。

I will create another class and make it my start class, where I will then call ExampleApplication and ExampleActivity.我将创建另一个 class 并将其作为我的开始 class,然后我将在其中调用 ExampleApplication 和 ExampleActivity。

How am I supposed to call two classes using Hilt dependencies from another class?我应该如何使用来自另一个 class 的 Hilt 依赖项调用两个类?

Of course, I have updated the manifest.xml so that it says.MainActivity.当然,我已经更新了 manifest.xml 以便它显示.MainActivity。

First of all, why do you need Hilt?首先,为什么需要Hilt? What are you trying to Inject and where?你想注入什么,在哪里注入?

ExampleApplication() is just a place where you configure app-wide settings, you are never supposed to call it from somewhere else, so you don't need that line. ExampleApplication() 只是您配置应用程序范围设置的地方,您永远不应该从其他地方调用它,因此您不需要该行。

Furthermore, Which activity are you trying to start?此外,您要开始哪项活动? MainActivity or ExampleActivity()? MainActivity 或 ExampleActivity()? Calling ExampleActivity within MainActivity won't have any effect.在 MainActivity 中调用 ExampleActivity 不会有任何效果。

SetContent {... } is the place where you create the UI. SetContent {... } 是您创建 UI 的地方。

If you want to open another activity from MainActivity then you need to implement navigation or use Intents.如果你想从 MainActivity 打开另一个活动,那么你需要实现导航或使用 Intents。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM