kotlinruntime-errorruntimeexceptionillegalargumentexception

This error happening with me when I run my application every time? (Kotlin)


Process: com.example.musicplayer, PID: 5692
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.musicplayer/com.example.musicplayer.MainActivity}: java.lang.IllegalArgumentException: Invalid token date_addedDESC
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8177)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Caused by: java.lang.IllegalArgumentException: Invalid token date_addedDESC
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:495)
at android.content.ContentResolver.query(ContentResolver.java:1229)
at android.content.ContentResolver.query(ContentResolver.java:1161)
at com.example.musicplayer.MainActivity.getAllAudio(MainActivity.kt:123)
at com.example.musicplayer.MainActivity.initializeLayout(MainActivity.kt:106)
at com.example.musicplayer.MainActivity.onCreate(MainActivity.kt:35)
at android.app.Activity.performCreate(Activity.java:8595)
at android.app.Activity.performCreate(Activity.java:8573)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3764)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loopOnce(Looper.java:205) 
at android.os.Looper.loop(Looper.java:294) 
at android.app.ActivityThread.main(ActivityThread.java:8177) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) 

Image of My code


Solution

  • You're calling contentResolver.query with a value of MediaStore.Audio.Media.DATE_ADDED + "DESC" for the parameter "sortOrder". Hence, it's no surprise that you're getting IllegalArgumentException: Invalid token "dateAddedDESC". You need to define a proper value for the sort order.

    By the way, I'm not an Android developer and yet I could work out the problem just by looking at the error message.