0

I just created a new project and added a simple empty coroutine just to check if things were fine .

class MainActivity : AppCompatActivity(),CoroutineScope {
    lateinit var job: Job
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        job=Job()
        launch {  }
    }
}

I get this crash

Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
2019-01-31 21:06:54.720 11930-11930/com.seed.kotlintest I/zygote64:     at void android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener(android.view.View, android.support.v4.view.OnApplyWindowInsetsListener) (ViewCompat.java:2203)
2019-01-31 21:06:54.720 11930-11930/com.seed.kotlintest I/zygote64:     at android.view.ViewGroup android.support.v7.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:637)
2019-01-31 21:06:54.720 11930-11930/com.seed.kotlintest I/zygote64:     at void android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
2019-01-31 21:06:54.720 11930-11930/com.seed.kotlintest I/zygote64:     at void android.support.v7.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
2019-01-31 21:06:54.720 11930-11930/com.seed.kotlintest I/zygote64:     at void android.support.v7.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
2019-01-31 21:06:54.720 11930-11930/com.seed.kotlintest I/zygote64:     at void com.seed.kotlintest.MainActivity.onCreate(android.os.Bundle) (MainActivity.kt:16)
2019-01-31 21:06:54.720 11930-11930/com.seed.kotlintest I/zygote64:     at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:7183)
2019-01-31 21:06:54.720 11930-11930/com.seed.kotlintest I/zygote64:     at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1220)
2019-01-31 21:06:54.720 11930-11930/com.seed.kotlintest I/zygote64:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2910)
2019-01-31 21:06:54.720 11930-11930/com.seed.kotlintest I/zygote64:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$Ac

My build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.seed.kotlintest"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0'
}

You guys can attempt to the same. Just create a new empty project and add the empty coroutine

4

1 回答 1

1

要使用Dispatchers.Main我们需要kotlinx-coroutines-androidbuild.gradle文件中添加:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0'
于 2019-01-31T17:54:38.067 回答