Estou tentando tentando executar uma ação no aplicativo Android ao clicar num elemento html na webview. Ao clicar no elemento, o evento é ativado mas o ocorre o seguinte erro no alert(err)
do javascript dentro do try catch:
Error: Error calling method on NPObject.
Erro no log do Android:
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)
Suponho que seja problema de escopo. Segue o meu código:
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);
}
}
}
A função showMenu()
é executada no Android. Coloquei um Toast para testar e ela é chamada, MENOS btnFinalizar.setVisibility(View.VISIBLE);
E aqui está meu JavaScript que está funcionando.
$("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); }
});
O que preciso fazer para o btnFinalizar.setVisibility(View.VISIBLE);
funcionar?
PS: este comando está funcional fora dessa classe WebAppInterface