方向更改后,菜单中的 TextView 始终为 null
TextView in menu is always null after orientation change
我有一个 TextView 用作菜单项上的通知标记。问题是 findViewById 方法在方向更改后总是 returns null,因此当我尝试使用 setVisibility 修改其可见性时,它会抛出 NullPointerException。我尝试通过在 onRestart 中调用 invalidateOptionsMenu 再次调用 onCreateOptionsMenu,但它似乎没有帮助。
据我所知,所有其他视图都已找到,只是这个 TextView 令人讨厌。
崩溃的部分(在onCreate中调用):
public void updateUnreadNotificationCount(final int unreadNotificationsCount) {
unreadNotificationCount = unreadNotificationsCount;
if (unreadNotificationCount == 0) {
notificationCounter.setVisibility(View.INVISIBLE);
}else {
notificationCounter.setVisibility(View.VISIBLE);
notificationCounter.setText(String.valueOf(unreadNotificationCount));
}
}
如何创建 Textview:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
View count = menu.findItem(R.id.action_notifications).getActionView();
notificationCounter = (TextView) count.findViewById(R.id.textview_notification_count);
count.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mCtx, NotificationListActivity.class);
startActivity(i);
}
});
notificationCounter.setText(String.valueOf(unreadNotificationCount));
return super.onCreateOptionsMenu(menu);
}
XML 菜单项:
<item
android:id="@+id/action_notifications"
android:title="@string/action_notifications"
android:icon="@drawable/ic_mail_white_48dp"
android:actionLayout="@layout/actionbar_notification_icon"
android:showAsAction="always"
app:showAsAction="always"
android:orderInCategory="60"/>
TextView 的动作布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
style="@android:style/Widget.ActionButton">
<ImageView
android:id="@+id/imageview_notification"
android:src="@drawable/ic_mail_white_48dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="0dp"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview_notification_count"
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/imageview_notification"
android:layout_alignRight="@id/imageview_notification"
android:layout_marginRight="0dp"
android:layout_marginTop="3dp"
android:paddingBottom="1dp"
android:paddingRight="4dp"
android:paddingLeft="4dp"
android:background="@drawable/rounded_square"/>
</RelativeLayout>
通过将 TextView 的初始化移动到 onPrepareOptionsMenu 并在 期间调用 invalidateOptionsMenu() 设法解决了这个问题onResume().
我有一个 TextView 用作菜单项上的通知标记。问题是 findViewById 方法在方向更改后总是 returns null,因此当我尝试使用 setVisibility 修改其可见性时,它会抛出 NullPointerException。我尝试通过在 onRestart 中调用 invalidateOptionsMenu 再次调用 onCreateOptionsMenu,但它似乎没有帮助。
据我所知,所有其他视图都已找到,只是这个 TextView 令人讨厌。
崩溃的部分(在onCreate中调用):
public void updateUnreadNotificationCount(final int unreadNotificationsCount) {
unreadNotificationCount = unreadNotificationsCount;
if (unreadNotificationCount == 0) {
notificationCounter.setVisibility(View.INVISIBLE);
}else {
notificationCounter.setVisibility(View.VISIBLE);
notificationCounter.setText(String.valueOf(unreadNotificationCount));
}
}
如何创建 Textview:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
View count = menu.findItem(R.id.action_notifications).getActionView();
notificationCounter = (TextView) count.findViewById(R.id.textview_notification_count);
count.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mCtx, NotificationListActivity.class);
startActivity(i);
}
});
notificationCounter.setText(String.valueOf(unreadNotificationCount));
return super.onCreateOptionsMenu(menu);
}
XML 菜单项:
<item
android:id="@+id/action_notifications"
android:title="@string/action_notifications"
android:icon="@drawable/ic_mail_white_48dp"
android:actionLayout="@layout/actionbar_notification_icon"
android:showAsAction="always"
app:showAsAction="always"
android:orderInCategory="60"/>
TextView 的动作布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
style="@android:style/Widget.ActionButton">
<ImageView
android:id="@+id/imageview_notification"
android:src="@drawable/ic_mail_white_48dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="0dp"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview_notification_count"
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/imageview_notification"
android:layout_alignRight="@id/imageview_notification"
android:layout_marginRight="0dp"
android:layout_marginTop="3dp"
android:paddingBottom="1dp"
android:paddingRight="4dp"
android:paddingLeft="4dp"
android:background="@drawable/rounded_square"/>
</RelativeLayout>
通过将 TextView 的初始化移动到 onPrepareOptionsMenu 并在 期间调用 invalidateOptionsMenu() 设法解决了这个问题onResume().