广播接收器来时更改菜单项图标
Posted
技术标签:
【中文标题】广播接收器来时更改菜单项图标【英文标题】:Change menuitem icon when broadcast receiver comes 【发布时间】:2014-05-06 08:12:18 【问题描述】:我的操作栏上有两个菜单项,一个是显示蓝牙已打开的图标,另一个是显示已打开的 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;
我知道问题可能是菜单值,但我不知道如何解决这个问题。这也是我的日志:
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】:这是更新菜单项的正确方法
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;
【讨论】:
invalidateOptionsMenu。这就是我尝试 onPrepareOptionsMenu 时所缺少的。谢谢m8。它就像一个冠军以上是关于广播接收器来时更改菜单项图标的主要内容,如果未能解决你的问题,请参考以下文章