3

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

I'm trying to experiment with Kotlin in Android app development. Here is my 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:

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.

<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

Sparker0i
  • 1,787
  • 4
  • 35
  • 60
  • Are you sure that the `import` in `SpeechApp.kt` (or wherever `SpeechApp` is defined) is pulling in `android.app.Application`, and not some other class named `Application`? – CommonsWare Jun 30 '18 at 20:23
  • @CommonsWare then don't you think, it would have been a compile time error? – Pavneet_Singh Jun 30 '18 at 20:24
  • @CommonsWare It is definitely pulling android.app.Application as I had indeed mentioned `import android.app.Application` inside `SpeechApp` – Sparker0i Jun 30 '18 at 20:25
  • @Pavneet_Singh: Quite possibly, but I'm just checking. – CommonsWare Jun 30 '18 at 20:30
  • 2
    i have no doubts about your method buddy, they are great and @Sparker0i, have tried clean build , might work – Pavneet_Singh Jun 30 '18 at 20:42
  • this happens sometimes. try cleaning your project or deleting app/build folder – mudit_sen Jun 30 '18 at 20:42
  • Theoretically this is only possible if you didn't update the app on your phone. Try uninstalling it from test device, clean/rebuild, then run again – EpicPandaForce Jun 30 '18 at 21:02
  • Why `android:name=".app.SpeechApp"` and not `android:name=".SpeechApp"`? And why `activity android:name=".activity.MainActivity"` and not `android:name=".MainActivity"` in your Manifest? Trying to replicate your app the code does not even compile. –  Jun 30 '18 at 21:40
  • @mTak because of package. `MainActivity` is in `me.sparker0i.speechcorrection.activity` and `SpeechApp` is in `me.sparker0i.speechcorrection.app` – Sparker0i Jul 01 '18 at 06:56

1 Answers1

0

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

Sparker0i
  • 1,787
  • 4
  • 35
  • 60