尽管在Manifest中提到了android.app.Application,但是无法进行转换

时间:2021-06-10 22:42:48

I have looked at all the solutions in *, but all of them are telling to add android:name in the application tag, which I have already done

我已经查看了*中的所有解决方案,但所有这些解决方案都告诉我在应用程序标记中添加android:name,我已经完成了

I'm trying to experiment with Kotlin in Android app development. Here is my MainActivity

我正在尝试在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
    }
}

In onCreate() last line, application refers to getApplication() method of Activity class. Here is my SpeechApp:

在onCreate()的最后一行,application引用了Activity类的getApplication()方法。这是我的SpeechApp:

class SpeechApp: Application() {
    var isDictionaryRead = false
    lateinit var wordslist : ArrayList<String>

    override fun onCreate() {
        super.onCreate()

        Thread() {
            Runnable {
                /* Some Code here */
            }
        }.start()
    }
}

Now here is my Manifest. Do note the android:name specified there.

现在这是我的清单。请注意那里指定的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>

Despite having done all of that, I get this error:

尽管已经完成了所有这些,但我得到了这个错误:

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. Line 34 in MainActivity points to the line in onCreate() where I am giving the value to app

PS。 MainActivity中的第34行指向onCreate()中的行,我将值赋给app

1 个解决方案

#1


0  

Thanks guys, uninstalling and reinstalling did the job. Don't know what was happening before. Thanks for all your help

谢谢大家,卸载和重新安装完成了这项工作。不知道之前发生了什么。感谢你的帮助

#1


0  

Thanks guys, uninstalling and reinstalling did the job. Don't know what was happening before. Thanks for all your help

谢谢大家,卸载和重新安装完成了这项工作。不知道之前发生了什么。感谢你的帮助