android.app.Application 尽管在清单中被提及,但仍无法施放

android.app.Application cannot be cast despite being mentioned in the Manifest

我已经查看了 Whosebug 中的所有解决方案,但它们都告诉我在应用程序标签中添加 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指的是Activityclass的getApplication()方法。这是我的 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

谢谢大家,卸载并重新安装就可以了。不知道之前发生了什么。感谢您的帮助