0
I’m trying to perform an action in the Android app by clicking an html element in the webview. When clicking on the element, the event is activated but the following error occurs in the alert(err)
javascript inside the Try catch:
Error: Error Calling method on Npobject.
Error in Android log:
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’m guessing it’s a scope problem. Just follow 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 function showMenu()
is run on Android. I put a Toast to test and it is called, LESS btnFinalizar.setVisibility(View.VISIBLE);
And here’s my Javascript that’s 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 I need to do for the btnFinalizar.setVisibility(View.VISIBLE);
work?
PS: this command is functional outside that class WebAppInterface