我正在尝试在购物车图标上添加一个计数器
public boolean onCreateOptionsMenu(final Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
menu.clear();
getMenuInflater().inflate(R.menu.main, menu);
RelativeLayout badge =(RelativeLayout)menu.findItem(R.id.action_cart).getActionView();
ui_hot = (TextView) badge.findViewById(R.id.count);
}
return super.onCreateOptionsMenu(menu);
}
但我收到以下消息:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.RelativeLayout.findViewById(int)' on a null object reference
以下是Xml菜单文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_cart"
android:title="Test Counter Action"
android:orderInCategory="1"
android:actionLayout="@layout/cart_update_count"
app:showAsAction="always" />
</menu>
答案 0 :(得分:-3)
这可能会帮助你解决问题。
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<Textview
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myTextview"
android:background="#000000" /> </android.support.v7.widget.Toolbar>`**