用工具栏替换操作栏后出现 NullPointerException
NullPointerException after replaceing actionbar with toolbar
我在一个菜单项中使用徽章布局
这是一些菜单项
<item
android:id="@+id/action_rdvmed"
android:title="@string/rdv"
android:icon="@drawable/ic_action_action_event"
android:showAsAction="always" />
<item
android:id="@+id/action_msg"
android:title="@string/msg"
android:actionLayout="@layout/badge_layout" <!--this one causes the problem-->
android:showAsAction="always" />
<item
android:id="@+id/action_actualiser"
android:title="@string/actualiser"
android:icon="@drawable/ic_action_refresh"
android:showAsAction="always" />
我在 onCreateOptionMenu 中遇到空指针异常
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_med, menu);
this.MenuMed = menu;
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.action_msg).getActionView();
mCounter = (TextView) badgeLayout.findViewById(R.id.hotlist_hot); //the error in this line
imgMessage = (ImageView) badgeLayout.findViewById(R.id.hotlist_bell);
badgeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listMessages.clear();
msg();
}
});
return true;
}
这是我正在使用工具栏的 muy onCreate 方法的一部分
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_med);
Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
提前致谢。
我相信您遇到的问题与一位用户遇到的问题相同 。
技巧 是您需要将 android:actionLayout
更改为 app:actionLayout
。
所以,改变你的
<item
android:id="@+id/action_msg"
android:title="@string/msg"
android:actionLayout="@layout/badge_layout"
android:showAsAction="always" />
到,
<item
android:id="@+id/action_msg"
android:title="@string/msg"
app:actionLayout="@layout/badge_layout"
android:showAsAction="always" />
我在一个菜单项中使用徽章布局
这是一些菜单项
<item
android:id="@+id/action_rdvmed"
android:title="@string/rdv"
android:icon="@drawable/ic_action_action_event"
android:showAsAction="always" />
<item
android:id="@+id/action_msg"
android:title="@string/msg"
android:actionLayout="@layout/badge_layout" <!--this one causes the problem-->
android:showAsAction="always" />
<item
android:id="@+id/action_actualiser"
android:title="@string/actualiser"
android:icon="@drawable/ic_action_refresh"
android:showAsAction="always" />
我在 onCreateOptionMenu 中遇到空指针异常
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_med, menu);
this.MenuMed = menu;
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.action_msg).getActionView();
mCounter = (TextView) badgeLayout.findViewById(R.id.hotlist_hot); //the error in this line
imgMessage = (ImageView) badgeLayout.findViewById(R.id.hotlist_bell);
badgeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listMessages.clear();
msg();
}
});
return true;
}
这是我正在使用工具栏的 muy onCreate 方法的一部分
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_med);
Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
提前致谢。
我相信您遇到的问题与一位用户遇到的问题相同
技巧 是您需要将 android:actionLayout
更改为 app:actionLayout
。
所以,改变你的
<item
android:id="@+id/action_msg"
android:title="@string/msg"
android:actionLayout="@layout/badge_layout"
android:showAsAction="always" />
到,
<item
android:id="@+id/action_msg"
android:title="@string/msg"
app:actionLayout="@layout/badge_layout"
android:showAsAction="always" />