我正在尝试在操作栏中创建视图徽章。 (使用最新的支持库)。
我正在尝试以下方式。但它说空指针异常。
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_dashboard_menu, menu);
MenuItem textBadge = menu.findItem(R.id.badge);
RelativeLayout badgeLayout = (RelativeLayout) MenuItemCompat.getActionView(textBadge);
TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
tv.setText("12");
return super.onCreateOptionsMenu(menu);
}
Menu.xml是
<item
android:id="@+id/badge"
android:actionLayout="@layout/actionbar_badge_layout"
android:icon="@drawable/btn_star_off_disabled_holo_light"
android:showAsAction="always|withText">
</item>
,Layout.xml是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right" >
<!-- Menu Item Image -->
<ImageView
android:layout_width="48dp"
android:layout_height="fill_parent"
android:clickable="true"
android:src="@drawable/btn_star_off_disabled_holo_light" />
<!-- Badge Count -->
<TextView
android:id="@+id/actionbar_notifcation_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="5dp"
android:text="99"
android:textColor="#ffffff" />
</RelativeLayout>
这是我的日志:
01-22 09:12:45.439: E/AndroidRuntime(4819): FATAL EXCEPTION: main
01-22 09:12:45.439: E/AndroidRuntime(4819): java.lang.NullPointerException
01-22 09:12:45.439: E/AndroidRuntime(4819): at com.activities.DashboardActivity.onCreateOptionsMenu(DashboardActivity.java:617)
01-22 09:12:45.439: E/AndroidRuntime(4819): at android.app.Activity.onCreatePanelMenu(Activity.java:2158)
01-22 09:12:45.439: E/AndroidRuntime(4819): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
01-22 09:12:45.439: E/AndroidRuntime(4819): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
01-22 09:12:45.439: E/AndroidRuntime(4819): at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:70)
01-22 09:12:45.439: E/AndroidRuntime(4819): at android.os.Handler.handleCallback(Handler.java:587)
01-22 09:12:45.439: E/AndroidRuntime(4819): at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 09:12:45.439: E/AndroidRuntime(4819): at android.os.Looper.loop(Looper.java:130)
01-22 09:12:45.439: E/AndroidRuntime(4819): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-22 09:12:45.439: E/AndroidRuntime(4819): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 09:12:45.439: E/AndroidRuntime(4819): at java.lang.reflect.Method.invoke(Method.java:507)
01-22 09:12:45.439: E/AndroidRuntime(4819): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-22 09:12:45.439: E/AndroidRuntime(4819): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-22 09:12:45.439: E/AndroidRuntime(4819): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:14)
谢谢你的帮助。
对于所有寻求答案的人。
它在菜单XML中导致空指针异常。将android:actionLayout更改为app:actionLayout。
早些时候:
<item
android:id="@+id/badge"
android:actionLayout="@layout/actionbar_badge_layout"
android:icon="@drawable/btn_star_off_disabled_holo_light"
android:showAsAction="always|withText">
更改为:
<item
android:id="@+id/badge"
yourapp:actionLayout="@layout/actionbar_badge_layout"
android:icon="@drawable/btn_star_off_disabled_holo_light"
yourapp:showAsAction="always|withText">