stream-chat-android icon indicating copy to clipboard operation
stream-chat-android copied to clipboard

RuntimeException on initialising while offline

Open Chikrix opened this issue 7 years ago • 10 comments

When a device is offline and tries to initialise a viewmdodel like the ChannelListViewmodel, it throws a fatal RuntimeException with message Cannot create an instance of class com.getstream.sdk.chat.viewmodel.ChannelListViewModel

Chikrix avatar Nov 15 '19 16:11 Chikrix

@Chikrix do you have full stack trace? And do you have steps to reproduce? Sample app works correctly without network. View models are created internally by ViewModelProviders, so I doubt that it is somehow connected with offline mode. Also the most obvious reason is absence of public constructor: https://stackoverflow.com/questions/44998051/cannot-create-an-instance-of-class-viewmodel ChannelListViewModel has it.

elevenetc avatar Nov 18 '19 12:11 elevenetc

Closing it as inactive.

elevenetc avatar Nov 22 '19 12:11 elevenetc

java.lang.RuntimeException: Cannot create an instance of class com.getstream.sdk.chat.viewmodel.ChannelListViewModel i am getting this error whenever i try to assign viewmodel

abhirup-patra avatar Jun 04 '20 08:06 abhirup-patra

Hello @abhirup-patra do you have full stack trace? Could you provide code where you create and assign ChannelListViewModel

elevenetc avatar Jun 04 '20 09:06 elevenetc

sure

abhirup-patra avatar Jun 04 '20 09:06 abhirup-patra

i am using a channelListFragment where inside onCreateView i am assigning the viewModel

private ChannelListViewModel viewModel;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //....

viewModel = new ViewModelProvider(this).get(ChannelListViewModel.class);
FilterObject filter = Filters.INSTANCE.eq("type", "messaging");
viewModel.setQuery(filter,null);

//... }

abhirup-patra avatar Jun 04 '20 09:06 abhirup-patra

it previously worked alright when i used the sdk version 4.0.13 after upgrading to 4.2.10 beta i am facing this issue

abhirup-patra avatar Jun 04 '20 09:06 abhirup-patra

I am facing same problem with the channel activity as well with the message activity. there also i am facing problem with the view model

E/AndroidRuntime: FATAL EXCEPTION: main Process: abentra.com, PID: 32514 java.lang.RuntimeException: Unable to start activity ComponentInfo{abentra.com/abentra.com.views.fragments.stream_chat.ChannelActivity}: kotlin.UninitializedPropertyAccessException: lateinit property instance has not been initialized at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: kotlin.UninitializedPropertyAccessException: lateinit property instance has not been initialized at io.getstream.chat.android.livedata.ChatDomain$Companion.instance(ChatDomain.kt:140) at io.getstream.chat.android.livedata.ChatDomain.instance(Unknown Source:2) at com.getstream.sdk.chat.viewmodel.ChannelViewModel.(ChannelViewModel.java:109) at com.getstream.sdk.chat.viewmodel.ChannelViewModelFactory.create(ChannelViewModelFactory.java:23) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) at abentra.com.views.fragments.stream_chat.ChannelActivity.initViewModel(ChannelActivity.java:213) at abentra.com.views.fragments.stream_chat.ChannelActivity.onCreate(ChannelActivity.java:87) at android.app.Activity.performCreate(Activity.java:7802) at android.app.Activity.performCreate(Activity.java:7791) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)  at android.os.Handler.dispatchMessage(Handler.java:107)  at android.os.Looper.loop(Looper.java:214)  at android.app.ActivityThread.main(ActivityThread.java:7356)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

abhirup-patra avatar Jun 04 '20 09:06 abhirup-patra

ChannelViewModelFactory factory = new ChannelViewModelFactory(getApplication(), channelType, channelId); viewModel = new ViewModelProvider(this, factory).get(ChannelViewModel.class);

this is how i am initialising

abhirup-patra avatar Jun 04 '20 09:06 abhirup-patra

Initialisation of SDK is indeed has been changed since 4.0.13. It is still in beta and we're sorting out api, so breaking changes and bugs will appear.

You need to initialise ChatDomain to use viewmodels. You can see how it's done here: https://github.com/elevenetc/stream-chat-android-tutorials/blob/57d8809bbc031716b0c32e2f042eb915977b0e37/java/app/src/main/java/com/example/chattutorial/ChannelListActivity.java#L48

And here is channel activity: https://github.com/elevenetc/stream-chat-android-tutorials/blob/master/java/app/src/main/java/com/example/chattutorial/ChannelActivity.java

elevenetc avatar Jun 04 '20 11:06 elevenetc