I'm trying to try to perform an action in the Android application when I click on an html element in the webview. When clicking on the element, the event is activated but the following error occurs in alert(err)
of javascript inside try catch:
Error: Error calling method on NPObject.
Android log error:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6433)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:878)
at android.view.View.requestLayout(View.java:16683)
at android.view.View.requestLayout(View.java:16683)
at android.view.View.requestLayout(View.java:16683)
at android.view.View.requestLayout(View.java:16683)
at android.view.View.requestLayout(View.java:16683)
at android.view.View.requestLayout(View.java:16683)
at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
at android.view.View.requestLayout(View.java:16683)
at android.view.View.requestLayout(View.java:16683)
at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
at android.view.View.requestLayout(View.java:16683)
at android.view.View.setFlags(View.java:9055)
at android.view.View.setVisibility(View.java:6127)
at com.example.base.googlcloudvideointelligence.MainActivity$WebAppInterface.showMenu(MainActivity.java:131)
at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:24)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
I suppose it's a scope problem. Here is my code:
public class MainActivity extends AppCompatActivity {
Button btnFinalizar;
WebView webView;
WebSettings webSettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
btnFinalizar = (Button) findViewById(R.id.btnFinalizar);
webView = (WebView) findViewById(R.id.webView);
webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
webSettings.setAllowUniversalAccessFromFileURLs(true);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Log.d("MeuApp", consoleMessage.message() + " -- From line "
+ consoleMessage.lineNumber() + " of "
+ consoleMessage.sourceId());
return super.onConsoleMessage(consoleMessage);
}
});
webView.setWebViewClient(new WebViewClient ());
webView.loadUrl("http://urlexemplo.com.br");
}
public class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void showMenu() {
Toast.makeText(mContext, "AQUI FUNCIONA", Toast.LENGTH_SHORT).show();
btnFinalizar.setVisibility(View.VISIBLE);
}
}
}
The showMenu()
function runs on Android. I put a Toast to test and it is called, LESS btnFinalizar.setVisibility(View.VISIBLE);
And here is my JavaScript that is working.
$("body").on('click', '.step-question .go-step', function(event) {
event.preventDefault();
/* Act on the event */
try {
window.Android.showMenu();
alert('foi');
} catch(err) { alert(err); }
});
What do I need to do to btnFinalizar.setVisibility(View.VISIBLE);
work?
PS: this command is functional outside of this class WebAppInterface