0

我想在连接状态发生变化时更改标题和图标......这updateMenuTitle() 是从这里开始的并且可以工作,因为我已经对其进行了测试,但是当我调用updateMenuTitle()from 处理程序时应用程序在启动时崩溃时不起作用

 private void updateMenuTitle(int x) {
    MenuItem btstatus = menu.findItem(R.id.btstatus);
    if (x == 1) {
        btstatus.setTitle("Connected");
        btstatus.setIcon(R.drawable.ic_btstatus_on);
    } else if (x == 2){
        btstatus.setTitle("Connecting");
        btstatus.setIcon(R.drawable.ic_btstatus_idle);
    }else if (x == 0){
        btstatus.setTitle("Disconnected");
        btstatus.setIcon(R.drawable.ic_btstatus_off);
    }
}

private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case MESSAGE_STATE_CHANGE:
            switch (msg.arg1) {
            case BluetoothRfcommClient.STATE_CONNECTED:
                mTxtStatus.setText(R.string.title_connected_to);
                mTxtStatus.append(" " + mConnectedDeviceName);
                updateMenuTitle(1);//not working if I call it from here... app force close
                break;
            case BluetoothRfcommClient.STATE_CONNECTING:
                mTxtStatus.setText(R.string.title_connecting);
                updateMenuTitle(2);//not working if I call it from here... app force close
                break;
            case BluetoothRfcommClient.STATE_NONE:
                mTxtStatus.setText(R.string.title_not_connected);
                updateMenuTitle(0);//not working if I call it from here... app force close
                break;
            }
            break;
            .......

解决方案:

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (mChangedStat && mChangedStat2) {
            menu.findItem(R.id.btstatus).setTitle(R.string.title_connected);
            menu.findItem(R.id.btstatus).setIcon(R.drawable.ic_btstatus_on);
        } else if (!mChangedStat && mChangedStat2) {
            menu.findItem(R.id.btstatus).setTitle(R.string.title_connecting);
            menu.findItem(R.id.btstatus).setIcon(R.drawable.ic_btstatus_idle);
        } else if (!mChangedStat && !mChangedStat2) {
            menu.findItem(R.id.btstatus).setTitle(R.string.title_not_connected);
            menu.findItem(R.id.btstatus).setIcon(R.drawable.ic_btstatus_off);
        }
}

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MESSAGE_STATE_CHANGE:
                                switch (msg.arg1) {
            case BluetoothServiceClient.STATE_CONNECTED:
                mChangedStat = true;
                mChangedStat2 = true;
                supportInvalidateOptionsMenu();
                break;
            case BluetoothServiceClient.STATE_CONNECTING:
                mChangedStat = false;
                mChangedStat2 = true;
                supportInvalidateOptionsMenu();
                break;
            case BluetoothServiceClient.STATE_NONE:
                mChangedStat = false;
                mChangedStat2 = false;
                supportInvalidateOptionsMenu();
                break;
            }
                break;
                .......
4

2 回答 2

0

您可以为此实现 onPrepareOptionMenu() ,因为这更有效并且用户打开选项菜单,您可以显示带有标题和图标的所需菜单。

检查这个例子

http://thedevelopersinfo.wordpress.com/2009/10/20/dynamically-change-options-menu-items-in-android/

于 2013-02-11T10:44:25.403 回答
0

我做的有点不同:

private boolean flag = false;

void someMethod() {

   flag = true;
   invalidateOptionsMenu();

}

public boolean onCreateOptionsMenu (Menu menu) {

    if (flag) {
      // inflate menu 1
    } else {
      // inflate menu 2
    }

}

这样你就不需要处理消息什么的了。您只需要管理 onCreateOptionsMenu 上的菜单项。

于 2013-02-11T11:07:48.423 回答