Antonis Lambrianides Antonis Lambrianides - 5 days ago 5
Android Question

Change menuitem icon when broadcast receiver comes

I have two menu items on my actionbar, one is an icon showing that bluetooth is on, and the other one for wifi that it's on. I want to change the image to a different one when the network goes off and when the bluetooth goes off. So change images from on to off and vice versa.
I have a broadCastReceiver that tells me when I have a network or not. Now I keep getting null exceptions when I try to call the menuItem from within the broadcast receiver and change their image from there.

Here is my broadcast receiver:

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);
}
}
};


this is me onCreateOptionMenu:

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


I know the problem is probably the menu value but i cant figure it out how to fix this. ALso here is my 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

Answer

This is correct approach for updating menu items

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

and override one more 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;
    }
Comments