我在这里犯了一个大错误。 我正在尝试使用 supportInvalidateOptionsMenu() 更改操作栏菜单;但是当函数执行时,应用程序关闭,没有错误。
奇怪的是,在我的 Galaxy Nexus (4.2.2) 上一切正常,但在我 friend 的手机 (android 4.0.3) 和我的 android 2.1 模拟器中都不起作用
这是我的代码:
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();}
}
有人可以帮助我吗? :/
最佳答案
这很可能是线程问题。您应该看到有关主线程之外的 UI 访问的错误记录。也许你正在过滤掉它们?查看完整日志,而不仅仅是您的包的日志。
来自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.
尝试:
mActivity.runOnUiThread(new Runnable(){
@Override
public void run(){
mActivity.supportInvalidateOptionsMenu();
}
});
关于android - Sherlock Actionbar invalidateOptionsMenu(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14898940/