Flutter 无法实例化 Activity ComponentInfo Main Activity

问题描述 投票:0回答:4

我正在使用 vs code(编码)和 android studio(获取虚拟设备)。 我刚刚合并并提取了今天早上正在处理的项目,首先,要等待很长时间才能构建我的项目。所以我停止了所有操作,从虚拟设备中卸载了我的应用程序,然后通过“flutter run”进行清理和运行。现在我有这些错误:

Using hardware rendering with device AOSP on IA Emulator. If you notice graphics artifacts, consider enabling software rendering with
"--enable-software-rendering".
Launching lib\main.dart on AOSP on IA Emulator in debug mode...
Running Gradle task 'assembleDebug'...                              6,0s
√  Built build\app\outputs\flutter-apk\app-debug.apk.
E/AndroidRuntime(11190): FATAL EXCEPTION: main
E/AndroidRuntime(11190): Process: com.example.coming_soon, PID: 11190
E/AndroidRuntime(11190): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.coming_soon/com.example.coming_soon.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.coming_soon.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.coming_soon-g9Xk6gs-AdO4Khd-H_dR3g==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.coming_soon-g9Xk6gs-AdO4Khd-H_dR3g==/lib/x86, /data/app/com.example.coming_soon-g9Xk6gs-AdO4Khd-H_dR3g==/base.apk!/lib/x86, /system/lib]]
E/AndroidRuntime(11190):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
E/AndroidRuntime(11190):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
E/AndroidRuntime(11190):        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
E/AndroidRuntime(11190):        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
E/AndroidRuntime(11190):        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
E/AndroidRuntime(11190):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
E/AndroidRuntime(11190):        at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(11190):        at android.os.Looper.loop(Looper.java:193)
E/AndroidRuntime(11190):        at android.app.ActivityThread.main(ActivityThread.java:6669)
E/AndroidRuntime(11190):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(11190):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/AndroidRuntime(11190):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
E/AndroidRuntime(11190): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.coming_soon.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.coming_soon-g9Xk6gs-AdO4Khd-H_dR3g==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.coming_soon-g9Xk6gs-AdO4Khd-H_dR3g==/lib/x86, /data/app/com.example.coming_soon-g9Xk6gs-AdO4Khd-H_dR3g==/base.apk!/lib/x86, /system/lib]]
E/AndroidRuntime(11190):        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
E/AndroidRuntime(11190):        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
E/AndroidRuntime(11190):        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
E/AndroidRuntime(11190):        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
E/AndroidRuntime(11190):        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
E/AndroidRuntime(11190):        at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
E/AndroidRuntime(11190):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
E/AndroidRuntime(11190):        ... 11 more

这是我的颤抖医生-v


[√] Flutter (Channel stable, 2.5.3, on Microsoft Windows [version 10.0.19043.1348], locale fr-FR)
    • Flutter version 2.5.3 at C:\Users\Johanne\Development\sdks\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 18116933e7 (5 weeks ago), 2021-10-15 10:46:35 -0700
    • Engine revision d3ea636dc5
    • Dart version 2.14.4

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\Johanne\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] VS Code (version 1.62.2)
    • VS Code at C:\Users\Johanne\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.28.0

[√] Connected device (3 available)
    • AOSP on IA Emulator (mobile) • emulator-5556 • android-x86    • Android 9 (API 28) (emulator)
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 95.0.4638.69
    • Edge (web)                   • edge          • web-javascript • Microsoft Edge 95.0.1020.44

• No issues found!

这是我的 android.manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.coming_soon">
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
    <uses-permission android:name="android.permission.VIBRATE" />   
<application
        android:label="coming_soon"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

我已经搜索并发现了类似的问题,但我无法解决我的问题。 我尝试创建 MainActivity.java 文件,但出现新错误,因此我将其删除。 我错过了什么吗?

java android flutter visual-studio-code android-activity
4个回答
5
投票

对我来说,解决方案是修复 MainActivity.kt 中的包名称,由于某种原因,最后一部分被加倍。


4
投票

如果它对某人有帮助,对我来说,修复方法是进入 Android 文件夹下的 MainActivity.kt 文件并修复包导入行,以便它使用正确的应用程序 ID 名称,即 com.company.app。


3
投票

对我来说,解决方案是简单地在项目目录中运行

flutter clean
flutter pub get


0
投票

对我来说,我遇到了这个问题,因为我重命名了 flutter 应用程序的 Id,并且没有更新

MainActivity.kt
上的命名空间。您还必须重命名具有
MainActivity.kt
的文件夹以匹配您拥有的新 ID,即
android/app/src/main/kotlin/com/example/<your_new_appId>/MainActivity.kt

来自:

package com.example.<your_old_appId>

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}

至:

package com.example.<your_new_appId>

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}
© www.soinside.com 2019 - 2024. All rights reserved.