广播接收器到来时更改menuitem图标

时间:2014-05-06 08:12:18

标签: android menu broadcastreceiver menuitem

我的操作栏上有两个菜单项,一个是显示蓝牙已打开的图标,另一个是用于wifi的图标。我希望在网络熄灭和蓝牙熄灭时将图像更改为其他图像。因此,将图像从开启更改为关闭,反之亦然。 我有一个broadCastReceiver告诉我什么时候有网络。现在,当我尝试从广播接收器中调用menuItem并从那里更改图像时,我一直得到null异常。

这是我的广播接收器:

private BroadcastReceiver broadcastBufferReceiverCheckConnection = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent bufferIntent) {
        isInternetPresent = ConnectionDetector.isConnectingToInternet();
        MenuItem iconNetwork = menu.findItem(R.id.iconNetwork);
        if (isInternetPresent == false) {
            iconNetwork.setIcon(R.drawable.network_disconnected);
        } else {
            iconNetwork.setIcon(R.drawable.network_connected);
        }
    }
};

这是我onCreateOptionMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    this.menu = menu;
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

我知道问题可能是菜单值,但我无法弄清楚如何解决这个问题。这里也是我的logcat:

05-06 11:08:57.958: E/AndroidRuntime(16077): FATAL EXCEPTION: main
05-06 11:08:57.958: E/AndroidRuntime(16077): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x8000010 (has extras) } in petrolina.pptaviation.MainActivity$1@4147ecc0
05-06 11:08:57.958: E/AndroidRuntime(16077):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:798)
05-06 11:08:57.958: E/AndroidRuntime(16077):    at android.os.Handler.handleCallback(Handler.java:725)
05-06 11:08:57.958: E/AndroidRuntime(16077):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 11:08:57.958: E/AndroidRuntime(16077):    at android.os.Looper.loop(Looper.java:153)
05-06 11:08:57.958: E/AndroidRuntime(16077):    at android.app.ActivityThread.main(ActivityThread.java:5299)
05-06 11:08:57.958: E/AndroidRuntime(16077):    at java.lang.reflect.Method.invokeNative(Native Method)
05-06 11:08:57.958: E/AndroidRuntime(16077):    at java.lang.reflect.Method.invoke(Method.java:511)
05-06 11:08:57.958: E/AndroidRuntime(16077):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-06 11:08:57.958: E/AndroidRuntime(16077):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
05-06 11:08:57.958: E/AndroidRuntime(16077):    at dalvik.system.NativeStart.main(Native Method)
05-06 11:08:57.958: E/AndroidRuntime(16077): Caused by: java.lang.NullPointerException
05-06 11:08:57.958: E/AndroidRuntime(16077):    at petrolina.pptaviation.MainActivity$1.onReceive(MainActivity.java:417)
05-06 11:08:57.958: E/AndroidRuntime(16077):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:788)
05-06 11:08:57.958: E/AndroidRuntime(16077):    ... 9 more

1 个答案:

答案 0 :(得分:1)

这是更新菜单项的正确方法

private BroadcastReceiver broadcastBufferReceiverCheckConnection = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent bufferIntent) {
            isInternetPresent = ConnectionDetector.isConnectingToInternet();
            invalidateOptionsMenu();//Activity method
    }
};

并覆盖另一个方法

public boolean onPrepareOptionsMenu(Menu menu) {
       MenuItem iconNetwork = menu.findItem(R.id.iconNetwork);
       if (iconNetwork != null) {
           iconNetwork.setIcon(isInternetPresent ? R.drawable.network_connected : R.drawable.network_disconnected);
       }
       return true;
    }