[英]Change data from html page with Javascript in WebView
In my Android application
, i need to use a WebView
for one function. 在我的
Android application
,我需要为一个功能使用WebView
。 But i want translate this webpage . 但是我想翻译这个网页 。 So i've make a
Javascript
code for remplace "MyValueEN"
in "MyValueFR"
with string.xml
but i can't use getResources.GetString(...)
. 所以我做
Javascript
代码remplace "MyValueEN"
在"MyValueFR"
与string.xml
,但我不能使用getResources.GetString(...)
Exemple of my code: 我的代码示例:
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
When i call getResources().getString(R.string.MyTextToChange")
, i've got an 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
Can i use getRessources()
in Javascript
code ? 我可以在
Javascript
代码中使用getRessources()
吗? Did i have do an error ? 我做错了吗? Or i need use another function ?
或者我需要使用其他功能?
Thanks for your help 谢谢你的帮助
you will need to initialise 您将需要初始化
getResources().getString(R.string.MyTextToChange)
inside onCreate, incase you initialised it outside 在onCreate内部,以防您在外部对其进行初始化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.