Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sherlock Actionbar invalidateOptionsMenu()

I'm with a big error here. I'm trying to change the actionbar menus with the supportInvalidateOptionsMenu(); but when the function is executed, the application closes, without errors.

The strange thing is that everything works normally on my Galaxy Nexus (4.2.2), but does not work in my friend's mobile (android 4.0.3) nor in my emulator with android 2.1

Here is my code:

protected void onCreate(Bundle savedInstanceState) {
...
actionbar = getSupportActionBar();
...
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.loadUrl(getString(R.string.site_load));
...
}

public class WebAppInterface {
SherlockActivity mActivity;
WebAppInterface(SherlockActivity c) {mActivity = c;}  
public void setrefreshon() {showRefresh = true; mActivity.supportInvalidateOptionsMenu();}
}

Can someone help me? :/

like image 711
Artur Couto Avatar asked Feb 15 '13 16:02

Artur Couto


1 Answers

This is most likely a threading issue. You should be seeing logged errors about UI access outside of main thread. Perhaps you're filtering them out? Look at the full log, not just the log for your package.

From the Building Web Apps in Webview:

Note: The object that is bound to your JavaScript runs in another thread and not in the thread in which it was constructed.

Try:

mActivity.runOnUiThread(new Runnable(){
  @Override
  public void run(){
    mActivity.supportInvalidateOptionsMenu();
  }
});
like image 54
Delyan Avatar answered Nov 15 '22 09:11

Delyan