GetStream / stream-chat-android

:speech_balloon: Android Chat SDK ➜ Stream Chat API. UI component libraries for chat apps. Kotlin & Jetpack Compose messaging SDK for Android chat
https://getstream.io/chat/sdk/android/
Other
1.48k stars 273 forks source link

lateinit property instance has not been initialized #480

Closed jetaggart closed 4 years ago

jetaggart commented 4 years ago

Version: 4.2.5

Hit an issue with ChannelViewModel:

     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.<init>(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 io.getstream.thestream.ChannelActivity.initViewModel(ChannelActivity.kt:59)

It looks like ChatViewModel is trying to reference something called ChatDomain. I'm unsure what this is. I have set up my client in a fairly vanilla way following the example here:

fun init(context: Context, user: String, credentials: BackendService.StreamCredentials) {
    val chat = Chat.Builder(credentials.apiKey, context)
        .logLevel(ChatLogLevel.ALL)
        .build()

    this.user = User(user)
    this.user.extraData["name"] = user
    this.client = chat.client
    this.client.setUser(this.user, credentials.token, object : InitConnectionListener() {
        override fun onSuccess(data: ConnectionData) {
            Log.i("MainActivity", "setUser completed")
        }

        override fun onError(error: ChatError) {
            Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show()
            Log.e("MainActivity", "setUser onError")
        }
    })
}

This was working on 4.0.13

tschellenbach commented 4 years ago

Calling something like this will help resolve this:

chatDomain = new ChatDomain.Builder(getContext().getApplicationContext(), Chat.getInstance().getClient(), user).offlineEnabled().userPresenceEnabled().build();

We need to change the builder flow for the Chat class to make this more explicit in the future

jetaggart commented 4 years ago

That worked, however I have no idea what this thing is. Do you mind explaining it a bit? Not sure why I need to initialize a Chat instance and a ChatDomain instance. Wouldn't the Chat instance initialize this, or visa versa?

Also, hit another bug right after this:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.getstream.thestream, PID: 30702
    java.lang.IllegalArgumentException: width and height must be > 0
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1113)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1080)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1030)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:991)
        at com.getstream.sdk.chat.view.AvatarView.createImageRounded(AvatarView.kt:87)
        at com.getstream.sdk.chat.view.AvatarView.createBitmap(AvatarView.kt:68)
        at com.getstream.sdk.chat.view.AvatarView$createBitmap$2.invokeSuspend(Unknown Source:12)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
        at android.os.Handler.handleCallback(Handler.java:907)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7478)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)

Looks like this one? https://github.com/GetStream/stream-chat-android/issues/478

tschellenbach commented 4 years ago

@samiuelson on our team is working on cleaning up the initialization of the Chat instance.

The image loading bug is already fixed I believe, @JcMinarro

jetaggart commented 4 years ago

Awesome, thank you! Do users need to understand ChatDomain (writing a post for the blog)?

tschellenbach commented 4 years ago

Yes, but it's not 100% ready yet for an announcement, still polishing things a bit: https://getstream.io/chat/docs/livedata/?language=js

JcMinarro commented 4 years ago

Hi @psylinse the bug related with the bitmap creation is already fixed, you can use the version 2.4.6 According to the issue with ChatDomain, try to use the snippet code Thierry posted. BTW, we are working to simplify how it is initialized and will be easier to use soon

jetaggart commented 4 years ago

The snippet works great along with 2.4.6 (other issues not withstanding), just don't understand chat domain. I'll hold on for now, thank you!

tschellenbach commented 4 years ago

Thanks again for reporting this. New version of the builder is in progress in the "develop" branch. Will go live in the coming weeks. Looking forward to that release.

Sibm02021 commented 2 years ago

I've come across this issue again with a similar stack trace.

java.lang.IllegalStateException: ChatDomain.Builder::build() must be called before obtaining ChatDomain instance
    at io.getstream.chat.android.livedata.ChatDomain$Companion.instance(ChatDomain.kt:637)
    at io.getstream.chat.android.ui.channel.list.header.viewmodel.ChannelListHeaderViewModel.<init>(ChannelListHeaderViewModel.kt:17)
    at io.getstream.chat.android.ui.channel.list.header.viewmodel.ChannelListHeaderViewModel.<init>(Unknown Source:2)
    at java.lang.Class.newInstance(Native Method)
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
    at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
    at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112)
    at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:146)
    at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:111)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
    at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
    at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
    at com.example.inappchat.ChannelFragment.setupChannels$lambda-2(ChannelFragment.kt:109)
    at com.example.inappchat.ChannelFragment.setupChannels(ChannelFragment.kt:111)
    at com.example.inappchat.ChannelFragment.onCreateView(ChannelFragment.kt:59)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1764)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701)
    at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:488)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    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)

I've created the ChatClient class using a dependency injection framework, however when I build the ChatDomain interface in the application class I get following the stack trace. Everything works fine if channels are disabled in the app.

class MyApplication : Application() {

    @Inject
    lateinit var client : ChatClient

    override fun onCreate() {
        super.onCreate()
        ChatDomain.Builder(client, this).build()

    }

The complier seems to be telling me that the issue is caused by this line:

listHeaderViewModel.bindView(binding.channelListHeaderView, viewLifecycleOwner)
adasiewiczr commented 2 years ago

hey @Sibm02021, can you tell me which SDK version are you using? Make sure you are building io.getstream.chat.android.livedataChatDomain. You can also find io.getstream.chat.android.offline.ChatDomain but you need to initialize the first one in order to use UI components. We are currently working on next major release which will simplify the process and prevent issues like that.

Sibm02021 commented 2 years ago

@adasiewiczr I was using the latest one, however the issue has now disappeared and the chat is working fine. I think the issue was due to something I did wrong as opposed to any issues with the SDK.