从AlertDialog更改工具栏按钮图标"无法解析符号"

时间:2018-03-25 09:21:07

标签: android alertdialog android-toolbar

面对无法从AlertDialog更改工具栏按钮图标。 ("无法解析符号")。
试图在代码中的其他位置声明工具栏按钮并且没有结果。请帮忙解决。我该如何声明此按钮以避免此错误?

//Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);

    MenuItem mChangeCur = menu.findItem(R.id.change_cur);
    mChangeCur.setIcon(R.drawable.usd);

    MenuItem mSearch = menu.findItem(R.id.action_search);

    SearchView mSearchView = (SearchView) mSearch.getActionView();
    mSearchView.setQueryHint("Search");

    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }
        @Override
        public boolean onQueryTextChange(String newText) {
            adapter.getFilter().filter(newText);
            return true;
        }
    });
    return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
        case R.id.change_cur:
            // setup the alert builder
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Choose currency");
            // add a list
            String[] currencies = {"USD", "RUB", "AUD", "BRL", "CAD"};
            builder.setItems(currencies, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case 0:
                            cur = "USD";
                            new Refresh().execute();
                            mChangeCur.setIcon(R.drawable.rub); //Here is mistake
                            break;
                        case 1:
                            cur = "RUB";
                            new Refresh().execute();
                            mChangeCur.setIcon(R.drawable.rub); //Here is mistake
                            break;
                        case 2:
                        case 3:
                        case 4:

                        default:
                            break;
                    }
                }
            });

1 个答案:

答案 0 :(得分:0)

问题在于声明和初始化mChangeCur。我必须在类级别声明mChangeCur并在public boolean onCreateOptionsMenu(菜单菜单)中初始化