我已经看过StackOverflow中的所有解决方案,但所有这些都告诉我在应用程序标记中添加android:name
我正在尝试在Android应用程序开发中试用Kotlin。这是我的MainActivity
class MainActivity : AppCompatActivity() {
val REQ_CODE_SPEECH_INPUT = 100
var output: TextView? = null
lateinit var app : SpeechApp
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
output = findViewById(R.id.output)
app = application as SpeechApp
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_speech, menu)
return true
}
}
在onCreate()
的最后一行中,application
引用了getApplication()
类的Activity
方法。这是我的SpeechApp
:
class SpeechApp: Application() {
var isDictionaryRead = false
lateinit var wordslist : ArrayList<String>
override fun onCreate() {
super.onCreate()
Thread() {
Runnable {
/* Some Code here */
}
}.start()
}
}
现在这是我的清单。请注意此处指定的android:name
。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.sparker0i.speechcorrection">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".app.SpeechApp"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
尽管完成了所有这些操作,但出现此错误:
07-01 01:40:27.983 11892-11892/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.sparker0i.speechcorrection, PID: 11892
java.lang.RuntimeException: Unable to start activity ComponentInfo{me.sparker0i.speechcorrection/me.sparker0i.speechcorrection.activity.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to me.sparker0i.speechcorrection.app.SpeechApp
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6501)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to me.sparker0i.speechcorrection.app.SpeechApp
at me.sparker0i.speechcorrection.activity.MainActivity.onCreate(MainActivity.kt:34)
at android.app.Activity.performCreate(Activity.java:7036)
at android.app.Activity.performCreate(Activity.java:7027)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6501)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
PS。 MainActivity中的第34行指向onCreate()
中的行,其中我将值赋予app
答案 0 :(得分:0)
谢谢大家,卸载并重新安装完成了这项工作。不知道之前发生了什么。感谢您的所有帮助