Issue detail:
I am parsing the List using gson in kotlin with below syntax:
Best article to understand SingletonList Vs. List::of -- Collections :: Singleton List Showdown
Detailed error log Android Studio:
I am parsing the List using gson in kotlin with below syntax:
val methods: ArrayList<DataPOJO> = Gson().fromJson(dataObj.getString(withdraw_methods),
Array<DataPOJO>::class.java).toList() as ArrayList<DataPOJO>
Array<DataPOJO>::class.java).toList() as ArrayList<DataPOJO>
Here, It's working fine while API response "dataObj.getString(withdraw_methods)" string array contains more than one object.
In case of one object its giving below error: java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to java.util.ArrayList
In case of one object its giving below error: java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to java.util.ArrayList
Solution:
val methods: ArrayList<DataPOJO> = ArrayList(Gson().fromJson(dataObj.getString(withdraw_methods), Array<DataPOJO>::class.java).toList())
Best article to understand SingletonList Vs. List::of -- Collections :: Singleton List Showdown
Detailed error log Android Studio:
02-20 13:35:20.370 17010-17010/com.myandriodapp W/System.err: java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to java.util.ArrayList
02-20 13:35:20.372 1483-1507/? I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{c8e1655 u0 com.myandriodapp/.v3_kotlin.cashback.user_activity.ProfileActivity t262} time:51048610
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at com.myandriodapp.v3_kotlin.cashback.user_activity.WithdrawalFragment$onActivityCreated$5.onChanged(WithdrawalFragment.kt:199)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at com.myandriodapp.v3_kotlin.cashback.user_activity.WithdrawalFragment$onActivityCreated$5.onChanged(WithdrawalFragment.kt:50)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at com.myandriodapp.v3_kotlin.cashback.user_activity.viewmodel.WithdrawalViewModel$AppWithdrawMethodsAPI$1.invokeSuspend(WithdrawalViewModel.kt:101)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at android.os.Handler.handleCallback(Handler.java:742)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at android.os.Looper.loop(Looper.java:157)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5603)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
02-20 13:35:20.372 1483-1507/? I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{c8e1655 u0 com.myandriodapp/.v3_kotlin.cashback.user_activity.ProfileActivity t262} time:51048610
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at com.myandriodapp.v3_kotlin.cashback.user_activity.WithdrawalFragment$onActivityCreated$5.onChanged(WithdrawalFragment.kt:199)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at com.myandriodapp.v3_kotlin.cashback.user_activity.WithdrawalFragment$onActivityCreated$5.onChanged(WithdrawalFragment.kt:50)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at com.myandriodapp.v3_kotlin.cashback.user_activity.viewmodel.WithdrawalViewModel$AppWithdrawMethodsAPI$1.invokeSuspend(WithdrawalViewModel.kt:101)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at android.os.Handler.handleCallback(Handler.java:742)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at android.os.Looper.loop(Looper.java:157)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5603)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
No comments:
Post a Comment