dankito / RichTextEditor

Rich text WYSIWYG editor for Android and JavaFX
Apache License 2.0
92 stars 36 forks source link

[Crash] App crashes when using RichTextEditor in Hilt Fragment #49

Open TobyEb opened 4 years ago

TobyEb commented 4 years ago

Hi,

I integrated Hilt in my android app and now it crashes.

I'm using the latest version

implementation 'net.dankito.richtexteditor:richtexteditor-android:2.0.16'

This is the Stacktrace:

java.lang.ClassCastException: dagger.hilt.android.internal.managers.ViewComponentManager$FragmentContextWrapper cannot be cast to android.app.Activity at net.dankito.richtexteditor.android.RichTextEditor.onAttachedToWindow(RichTextEditor.kt:124) at android.view.View.dispatchAttachedToWindow(View.java:17558) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3332) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3339) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3339) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3339) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3339) at android.view.ViewGroup.addViewInner(ViewGroup.java:5016) at android.view.ViewGroup.addView(ViewGroup.java:4807) at androidx.fragment.app.FragmentContainerView.addView(FragmentContainerView.java:280) at android.view.ViewGroup.addView(ViewGroup.java:4747) at android.view.ViewGroup.addView(ViewGroup.java:4720) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:326) at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187) at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849) at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:101) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

it seems like you already made changes to this part of the code. is there any chance they get released soon?

Update:

I managed to make it work with following workaround:

val editorLayout = view.findViewById<LinearLayout>(R.id.mailEditorLayout)
this.mailTextEditor = RichTextEditor(requireActivity())
editorLayout.addView(this.mailTextEditor, 0)

anyway, it would be nice to add the editor in my xml layout

dankito commented 4 years ago

Thanks for reporting this bug to me!

Also wasn't aware of Hilt. Sounds really great, thanks for that one, also going to use it soon.

The issue is, the Context provided by Hilt is only a ContextWrapper, but not an Activity (Activity is derived from ContextWrapper).

Version 2.0.17 should fix this. Should be visible at Maven Central in maximum two hours.

Please check if that versions fixes the issue for you (and if so it would be kind if you could close this ticket).

TobyEb commented 4 years ago

hi,

unfortunatly the build fails after updating to version 2.0.17.

i get the following error:

Android resource compilation failed
AGPBI: {"kind":"error","text":"Android resource compilation failed","sources":[{"file":"...\jetified-android-utils-1.1.0\\res\\values\\values.xml","position":{"startLine":56,"startColumn":4,"startOffset":3368,"endLine":89,"endColumn":24,"endOffset":4566}}],"original":"...\jetified-android-utils-1.1.0\\res\\values\\values.xml:57:5-90:25: AAPT: error: duplicate value for resource 'attr/backgroundColor' with config ''.\n    ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource compilation failed","sources":[{"file":"...\jetified-android-utils-1.1.0\\res\\values\\values.xml","position":{"startLine":56,"startColumn":4,"startOffset":3368,"endLine":89,"endColumn":24,"endOffset":4566}}],"original":"...\jetified-android-utils-1.1.0\\res\\values\\values.xml:57:5-90:25: AAPT: error: resource previously defined here.\n    ","tool":"AAPT"}

the file is located at

net.dankito.utils:android-utils-1.1.0\res\values\values.xml

hope this helps, if you need more information feel free to ask!

dankito commented 4 years ago

Actually fixed this bug in AndroidUtils already in April, but never pushed it to Maven Central.

Version 2.0.18 should fix that now ... but depending on your chosen activity theme may introduces that bug: https://github.com/dankito/RichTextEditor/issues/53.

See there how to circumvent this.

Sorry for the bad code quality the last few weeks, but i rarely have time and do the changes hastily late in the night.

It will take at least three weeks till i will find time again so that i can dig deeply into the code and find out what the issue is.

TobyEb commented 4 years ago

Hi,

sorry for my late response, i only found some time today to check this.

no need to apologize. i think you did a great job!

the crash is fixed for now but i found some new issue which is also connected to this one.

Context.asActivity() works like charm and returns now the correct activity.

but

RichtextEditor.setHTML() stopped working for me with version 2.0.18

i dived into your code and i think the problem is AndroidJavaScriptExecutor line 60 in executeJavaScriptInLoadedEditor()

there is a check if context is Activity, if not, it doesn`t execute the script

    private fun executeJavaScriptInLoadedEditor(javaScript: String, resultCallback: ((String) -> Unit)? = null) {
        if(Looper.myLooper() == Looper.getMainLooper()) {
            executeScriptOnUiThread(javaScript, resultCallback)
        }
        else if(context is Activity) {
            (context as? Activity)?.runOnUiThread { executeScriptOnUiThread(javaScript, resultCallback) }
        }
        else {
            log.error("Trying to execute Script '$javaScript', but activity is null")
        }
    }
dkshin commented 3 years ago

same issue...