在操作栏图标上添加文本时出现java.lang.NullPointerException

时间:2015-04-17 10:51:44

标签: android xml

我在布局下有actionbar_badge_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="@mipmap/icon_back" />

<!-- Badge Count -->
<TextView
    android:id="@+id/actionbar_notifcation_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="99"
    android:textColor="@color/primary_dark_material_light" />

内部res/menu_comment xml我有:

<item
    android:id="@+id/action_add"
    android:icon="@mipmap/ic_menu_add"
    android:actionLayout="@layout/actionbar_badge_layout"
    android:title="Add"
    app:showAsAction="always">
</item>

然后在我的活动中,我onCreateOptionsMenu()为:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_comment, menu);

    RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.action_add).getActionView();
    TextView t = (TextView)badgeLayout.findViewById(R.id.actionbar_notifcation_textview); //here I am getting null pointer exception

    t.setText("12");


    return true;

}

但是我得到了NullPointer异常。 Logcat给出输出为:

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.RelativeLayout.findViewById(int)' on a null object reference
        at com.mypackage.activities.ShowCommentsActivity.onCreateOptionsMenu(ShowCommentsActivity.java:77)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2820)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
        at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
        at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
        at android.support.v7.widget.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:49)
        at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:459)
        at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:69)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

此代码有什么问题。任何建议将不胜感激。谢谢你的推荐

2 个答案:

答案 0 :(得分:4)

你在哪里关闭RelativeLayout

<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="@mipmap/icon_back" />

<!-- Badge Count -->
<TextView
    android:id="@+id/actionbar_notifcation_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="99"
    android:textColor="@color/primary_dark_material_light" />

</RelativeLayout>文件的末尾添加XML

另外,请尝试更改

  <item
    android:id="@+id/action_add"
    android:icon="@mipmap/ic_menu_add"
    android:actionLayout="@layout/actionbar_badge_layout"
    android:title="Add"
    app:showAsAction="always">
</item>

要,

<item
    android:id="@+id/action_add"
    android:icon="@mipmap/ic_menu_add"
    app:actionLayout="@layout/actionbar_badge_layout"
    android:title="Add"
    app:showAsAction="always">
</item>

答案 1 :(得分:1)

尝试从onCreate()调用以下函数:

private void setActiobarTitle(String title)
{
   View v = getActionbar().getCustomView();
   TextView t = (TextView) v.findViewById(R.id.actionbar_notifcation_textview);
   t.setText("12");
}

注意:同时设置actionBar.setDisplayShowCustomEnabled(true);以在操作栏中启用自定义布局。