工具栏菜单项错误 [英] ToolBar MenuItem error

查看:260
本文介绍了工具栏菜单项错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置购物篮图标放入购物车功能。当我运行应用程序,我得到这个错误


  

尝试调用虚拟方法android.view.View
  android.widget.RelativeLayout.findViewById(INT)的空对象
  参照


在这里,我使用AppCompatActivity。

我的code是在这里:
MainActivity.java

  @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
         。getMenuInflater()膨胀(R.menu.main,菜单);
        RelativeLayout的badgeLayout =(RelativeLayout的)menu.findItem(R.id.badge).getActionView();
        mCounter =(TextView中)badgeLayout.findViewById(R.id.counter);
        返回true;
}

menu.xml文件:

 <项目
    机器人:ID =@ + ID /徽章
    机器人:actionLayout =@布局/ badge_layout
    机器人:标题=徽章
    应用:showAsAction =总是>
< /项目>

badge_layout.xml

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =48dp
        机器人:layout_height =FILL_PARENT
        机器人:layout_gravity =右>        < ImageView的
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =FILL_PARENT
            机器人:可点击=真
            机器人:SRC =@绘制/ ic_shopping_cart/>        <的TextView
            机器人:ID =@ + ID /计数器
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentRight =真
            机器人:填充=8DP
            机器人:文字=12
            机器人:TEXTSIZE =10SP
            机器人:文字颜色=@色/ colorPrimary/>
    < / RelativeLayout的>


解决方案

终于拿到了车图标。
替换此

 <项目
            机器人:ID =@ + ID /徽章
            机器人:actionLayout =@布局/ badge_layout//检查这一行
            机器人:标题=徽章
            机器人:actionViewClass =android.widget.RelativeLayout
            应用:showAsAction =总是>
        < /项目>

 <项目
        机器人:ID =@ + ID /徽章
        应用:actionLayout =@布局/ badge_layout//检查这一行(此处所做的更改)
        机器人:标题=徽章
        机器人:actionViewClass =android.widget.RelativeLayout
        应用:showAsAction =总是>
    < /项目>

I am trying to set Cart Basket icon for Add To Cart functionality. While I run the app I got this error

Attempt to invoke virtual method 'android.view.View android.widget.RelativeLayout.findViewById(int)' on a null object reference

Here I am using AppCompatActivity.

My code is here: MainActivity.java

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.main, menu);
        RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
        mCounter = (TextView) badgeLayout.findViewById(R.id.counter);
        return true;
}

menu.xml:

<item
    android:id="@+id/badge"
    android:actionLayout="@layout/badge_layout"
    android:title="Badges"
    app:showAsAction="always">
</item>

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" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:src="@drawable/ic_shopping_cart"/>

        <TextView
            android:id="@+id/counter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:padding="8dp"
            android:text="12"
            android:textSize="10sp"
            android:textColor="@color/colorPrimary" />
    </RelativeLayout>

解决方案

Finally Got the cart icon. Replace this

  <item
            android:id="@+id/badge"
            android:actionLayout="@layout/badge_layout"  //Check this LINE
            android:title="Badges"
            android:actionViewClass="android.widget.RelativeLayout"
            app:showAsAction="always">
        </item>

To

<item
        android:id="@+id/badge"
        app:actionLayout="@layout/badge_layout"  //Check this LINE(Changes made here)
        android:title="Badges"
        android:actionViewClass="android.widget.RelativeLayout"
        app:showAsAction="always">
    </item>

这篇关于工具栏菜单项错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆