簡體   English   中英

java.lang.RuntimeException:無法為 Kotlin MainActivity 實例化活動 ComponentInfo

[英]java.lang.RuntimeException: Unable to instantiate activity ComponentInfo for Kotlin MainActivity

這是我第一次使用 kotlin,出於某種原因,我必須在 MainActivity 中使用 kotlin 語言。 我通過代碼將我的 MainActivity 類更改為 kotlin 文件-> 將 Java 文件轉換為 Kotlin 文件但是當我運行我的代碼時,我會收到以下錯誤:

java.lang.RuntimeException: Unable to instantiate activity 
ComponentInfo{ir.mohammadhf.sevenautoshow/ir.mohammadhf.sevenautoshow.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "ir.mohammadhf.sevenautoshow.MainActivity" on path: DexPathList[[zip file "/data/app/ir.mohammadhf.sevenautoshow-b4uU0l0_OCV9z_7YwD1uHg==/base.apk"],nativeLibraryDirectories=[/data/app/ir.mohammadhf.sevenautoshow-b4uU0l0_OCV9z_7YwD1uHg==/lib/x86, /data/app/ir.mohammadhf.sevenautoshow-b4uU0l0_OCV9z_7YwD1uHg==/base.apk!/lib/x86, /system/lib, /system/vendor/lib]]
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2718)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "ir.mohammadhf.sevenautoshow.MainActivity" on path: DexPathList[[zip file "/data/app/ir.mohammadhf.sevenautoshow-b4uU0l0_OCV9z_7YwD1uHg==/base.apk"]

我搜索了類似的問題,但在大多數問題中,他們建議“在 Android 清單中聲明活動”。 但它已經為我完成了。

這是我的清單:

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

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:testOnly="false"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

這是我的主要活動:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    }
}

和 Build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.1"
    defaultConfig {
        applicationId "ir.mohammadhf.sevenautoshow"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'

    implementation "androidx.core:core-ktx:+"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21"

}
repositories {
    mavenCentral()
}

有什么我想做的事嗎?


此問題的解決方案

將此代碼放入項目 build.gradle:

buildscript {
    ext.kotlin_version = '1.3.21'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }


}

然后將這些插件添加到你的模塊 build.gradle 中:

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

然后同步,它將把 kotlin 的插件添加到你的 java 項目中

嘗試在開始時在 build.gradle 文件中應用 kotlin

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 28

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM