Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.RuntimeException:无法为Kotlin MainActivity实例化活动组件信息_Java_Android_Kotlin - Fatal编程技术网

java.lang.RuntimeException:无法为Kotlin MainActivity实例化活动组件信息

java.lang.RuntimeException:无法为Kotlin MainActivity实例化活动组件信息,java,android,kotlin,Java,Android,Kotlin,这是我第一次使用kotlin,出于某种原因,我不得不在主要活动中使用kotlin语言。 我通过Code->Convert Java file to kotlin file将MainActivity类更改为kotlin file 但是,当我运行代码时,会出现以下错误: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ir.mohammadhf.sevenautoshow/ir.mohammadhf.

这是我第一次使用kotlin,出于某种原因,我不得不在主要活动中使用kotlin语言。 我通过Code->Convert Java file to kotlin file将MainActivity类更改为kotlin file 但是,当我运行代码时,会出现以下错误:

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>
和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()
}
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
    }


}
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
有什么我没做的吗


此问题的解决方案

将此代码放在ind project 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()
}
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
    }


}
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
然后将这些插件添加到模块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()
}
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
    }


}
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

尝试重建/清理项目。我尝试了几次,但都没有成功。显示build.gradle并完成清单文件。确定。我更改了清单并添加了build.gradleeverything看起来都是正确的,也许可以尝试文件>无效缓存并重新启动android Studio当我放置这些行并单击“同步”时,它显示以下消息:错误:找不到id为“kotlin android extensions”的插件。我找到了原因。我应该在build.gradle(Project)中进行一些配置。非常感谢你。它对我有用