无法启动协同程序构建器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法启动协同程序构建器相关的知识,希望对你有一定的参考价值。
我刚刚创建了一个新项目,并添加了一个简单的空协程,以检查是否正常。
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
我的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'
以上是关于无法启动协同程序构建器的主要内容,如果未能解决你的问题,请参考以下文章