
我正在尝试为我的菜单项实现自定义布局并经历了许多解决方案,每当我尝试在为menuItem为actionLayout指定的布局中获取TextVIEw时,我将获得Null指针异常,因为getActionVIEw返回null.
home_menu:
<?xml version="1.0" enCoding="utf-8"?><menu xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto"> <item androID:ID="@+ID/action_notifications" androID:title="" app:showAsAction="always" androID:visible="true" androID:icon="@drawable/ic_notifications_black_24dp" androID:orderIncategory="3" androID:actionLayout="@layout/counter_action_bar_notification_icon" /> <!--androID:icon="@drawable/ic_notifications_black_24dp"--> <!--androID:actionLayout="@layout/bage_layout"--></menu>counter_action_bar_notification_icon
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="wrap_content" androID:layout_height="fill_parent" androID:orIEntation="vertical" androID:gravity="center" androID:layout_gravity="center" androID:clickable="true" > <ImageVIEw androID:ID="@+ID/hotList_bell" androID:src="@drawable/ic_notifications_black_24dp" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:gravity="center" androID:layout_margin="0dp" androID:contentDescription="bell" /> <TextVIEw androID:ID="@+ID/hotList_hot" androID:layout_wIDth="wrap_content" androID:minWIDth="17sp" androID:textSize="12sp" androID:textcolor="#ffffffff" androID:layout_height="wrap_content" androID:gravity="center" androID:text="@null" androID:layout_aligntop="@ID/hotList_bell" androID:layout_alignRight="@ID/hotList_bell" androID:layout_marginRight="0dp" androID:layout_margintop="3dp" androID:paddingBottom="1dp" androID:paddingRight="4dp" androID:paddingleft="4dp" androID:background="@drawable/bage_circle"/></relativeLayout>主要活动:
@OverrIDe public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.home_menu, menu); item = menu.findItem(R.ID.action_notifications); if(loginStatus==false){ item.setVisible(false); return false; } if(user!=null && !user.getUserType().equals("3")){ item.setVisible(false); return false; } final VIEw menu_hotList = MenuItemCompat.getActionVIEw(item); TextVIEw ui_hot =(TextVIEw) menu_hotList.findVIEwByID(R.ID.hotList_hot); ui_hot.setText(Integer.toString(13)); return true;}解决方法:
试试这个
item.setActionVIEw(R.layout.counter_action_bar_notification_icon); final VIEw menu_hotList = MenuItemCompat.getActionVIEw(item); TextVIEw ui_hot =(TextVIEw) menu_hotList.findVIEwByID(R.ID.hotList_hot); ui_hot.setText(Integer.toString(13)); 总结 以上是内存溢出为你收集整理的android – MenuItemCompat.getActionView返回Null全部内容,希望文章能够帮你解决android – MenuItemCompat.getActionView返回Null所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)