E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.material.components, PID: 11563
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.material.components.activity.dashboard.Dashboard.onCreateOptionsMenu(Dashboard.java:251)
这条线上的问题:
ui_hot = menu_hotlist.findViewById(R.id.hotlist_hot);
发生了什么?我试图在菜单中
findViewById
,但是android找不到对象引用。有人可以帮助我吗?谢谢。完整代码:
Dashboard.java:
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_dashboard, menu);
final View menu_hotlist = menu.findItem(R.id.action_notification).getActionView();
ui_hot = menu_hotlist.findViewById(R.id.hotlist_hot); //Problem location
updateHotCount(hot_number);
new MyMenuItemStuffListener(menu_hotlist, "Show hot message") {
@Override
public void onClick(View v) {
}
};
return super.onCreateOptionsMenu(menu);
}
menu_dashboard.xml:
<?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_search"
android:icon="@drawable/ic_search"
android:orderInCategory="100"
android:title="Search"
app:showAsAction="always" />
<item
android:id="@+id/action_notification"
android:actionLayout="@layout/action_bar_notifitcation_icon"
app:showAsAction="always"
android:icon="@drawable/ic_notifications"
android:title="Notifications"
/>
<item
android:id="@+id/lessons_discuss_with_teacher"
android:orderInCategory="100"
android:title="Ask Teacher List"
app:showAsAction="never" />
</menu>
action_bar_notification_icon.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center"
android:clickable="true"
style="@android:style/Widget.ActionButton">
<ImageView
android:id="@+id/hotlist_bell"
app:srcCompat="@drawable/ic_notifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="0dp"
android:contentDescription="bell"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
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/rounded_square"/>
</RelativeLayout>
最佳答案
当应用程序尝试使用具有空值的对象引用时,抛出NullPointerException
。
试试看
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_dashboard, menu);
MenuItem menuITEM = menu.findItem(R.id.action_notification);
View view = menuITEM.getActionView();
ui_hot = view.findViewById(R.id.hotlist_hot);
费耶
您应该致电
app:actionLayout