无法启动协程生成器



我刚刚创建了一个新项目并添加了一个简单的空协程,只是为了检查一切是否正常。

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 {  }
    }
}

我得到这个崩溃

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'
}

你们可以尝试同样的事情。只需创建一个新的空项目并添加空协程

要使用Dispatchers.Main我们需要将kotlinx-coroutines-android添加到build.gradle文件中:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0'

相关内容

  • 没有找到相关文章

最新更新