android – how to use geckoEngineView's loadData() method-ThrowExceptions

Exception or error:

I’m new with Mozilla android components and I want to know how should I use
geckoEngineView’s loadData() or loadUrl() method to load
HTML files from the asset folder in android development.
I use loadData() and loadUrl() method like this:

session.loadUrl("file:///android_asset/1.html")

session.loadData("<html><body>Example HTML content here</body></html>","text/html", "utf-8")

this is my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <mozilla.components.browser.toolbar.BrowserToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:background="#aaaaaa" />


        <mozilla.components.browser.engine.gecko.GeckoEngineView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/engineView"/>

    </LinearLayout>

</LinearLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

    private val engine: Engine by lazy {
        GeckoEngine(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val session = engine.createSession()
        engineView.render(session)

        session.loadUrl("file:///android_asset/1.html")
        //or
        session.loadData("<html><body>Example HTML content here</body> 
        </html>","text/html", "utf-8")

    }
    override fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet): View?=
        when (name) {
            EngineView::class.java.name -> engine.createView(context, attrs).asView()
            else -> super.onCreateView(parent, name, context, attrs)
        }
}

in both cases, an empty page is shown

webview.loadUrl(“file:///android_asset/1.html”) works like a charm in android webview.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *