簡體   English   中英

在WebView中使用Javascript從HTML頁面更改數據

[英]Change data from html page with Javascript in WebView

在我的Android application ,我需要為一個功能使用WebView 但是我想翻譯這個網頁 所以我做Javascript代碼remplace "MyValueEN""MyValueFR"string.xml ,但我不能使用getResources.GetString(...)

我的代碼示例:

String js_string = "javascript:\n" +
            "document.getElementsByClassName('Div1')[0].style.display='none';\n" +
            "document.getElementsByClassName('Div2')[0].style.display='none';void(0)" +
            "document.body.innerHTML = document.body.innerHTML.split(\"MyTextToChange\").join("+ getResources().getString(R.string.MyTextToChange) + ");void(0);"; //It's here the error

當我調用getResources().getString(R.string.MyTextToChange") ,出現錯誤:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test/com.test}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

我可以在Javascript代碼中使用getRessources()嗎? 我做錯了嗎? 或者我需要使用其他功能?

謝謝你的幫助

您將需要初始化

getResources().getString(R.string.MyTextToChange)

在onCreate內部,以防您在外部對其進行初始化

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM