接続状態が変化したときにタイトルとアイコンを変更したい...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;
.......