当它被称为回调函数时,Menu对象将变为null

时间:2015-12-03 13:50:02

标签: android

我需要在操作栏/工具栏中放置进度条。我可以通过以下链接(http://www.michenux.net/android-refresh-item-action-bar-circular-progressbar-578.html)来实现。 但是,为了显示和隐藏进度条,我面临一个问题,遵循我在这里使用的回调设计模式。以下是代码段 -

    public interface ICallBack {
        void setRefreshActionButtonState(boolean b);
    }

    public class MyFragment extends Fragment implements View.OnClickListener, ICallBack {
        private Menu optionsMenu;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            webView = (WebView) findViewById(R.id.main_web);

            webView.setWebViewClient(new MyWebViewClient());    
            webView.loadUrl("http://stackoverflow.com/");
        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            this.optionsMenu = menu;
            getActivity().getMenuInflater().inflate(R.menu.menu_home_base, menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.action_refresh) {
                // Logic to load the webview again 
            }
            return super.onOptionsItemSelected(item);
        }

        @Override
        public void setRefreshActionButtonState(boolean refreshing) {
            if (optionsMenu != null) {
                final MenuItem refreshItem = optionsMenu.findItem(R.id.action_refresh);
                if (refreshItem != null) {
                    if (refreshing) {
                        refreshItem.setActionView(R.layout.toolbar_indeterminate_progress);
                    } else {
                        refreshItem.setActionView(null);
                    }
                }
            }
        }
    }


public class MyWebViewClient extends WebViewClient {
        ICallBack callBack;
        public MyWebViewClient(Activity context) {
            callBack = new MyFragment();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            callBack.setRefreshActionButtonState(true);//Showing the progressbar
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            callBack.setRefreshActionButtonState(false);//Hiding the progressbar
        }
    }

setRefreshActionButtonState()被调用时,我将 optionsMenu 值设为 null ,导致无法显示和隐藏进度条。

有人可以帮助我弄清楚我做错了吗。

谢谢,

1 个答案:

答案 0 :(得分:0)

您可以上传项目action_refresh所在的xml吗?问题可能出在findItem(R.id.action_refresh);