带有导航抽屉的操作栏中的图像视图



>我正在使用导航抽屉,我定义了一个主题来更改操作栏颜色。 现在我想向我的操作栏添加一个图像菜单(铃铛)以接收通知。应用程序在图像视图上粉碎我的日志点的问题ImageView bell= (ImageView)badgeLayout.findViewById(R.id.bell);

这是日志:

04-29 16:38:14.835  10481-10481/com.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.nearby.project.princ.Main.onCreateOptionsMenu(Main.java:520)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2553)
        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.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:979)
        at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
        at android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:79)
        at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:118)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
        at dalvik.system.NativeStart.main(Native Method)

这是我的主题:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
    parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
    parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/blue_pressed_want</item>
    <item name="android:windowNoTitle">false</item>
    <item name="android:windowActionBar">true</item>
    <item name="windowActionBar">true</item>
    <!-- Support library compatibility -->
    <item name="background">@color/blue_pressed_want</item>
</style>

这是我的主要活动(oncreateOptionMenu方法):

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();//getSupportMenuInflater();
    getMenuInflater().inflate(R.menu.activity_main, menu);
    RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.menu_notif).getActionView();
    ImageView bell= (ImageView)badgeLayout.findViewById(R.id.bell); //here the error

   // bell.setImageResource(R.drawable.notification);
    if (sharedpreferences.contains(keynotifcount)) {
        countnotif = sharedpreferences.getString(keynotifcount, "");
        mNotifCount= Integer.parseInt(countnotif);
    }
    if(mNotifCount!=0) {
        TextView notifCount = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
        notifCount.setVisibility(View.VISIBLE);
        notifCount.setText(String.valueOf(mNotifCount));
        etat=true;
    }
    if(mNotifCount==0) {
        TextView notifCount = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textviewoff);
        etat=false;
    }
    Toast.makeText(this, "notifcount" + mNotifCount, Toast.LENGTH_SHORT).show();
    bell.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //startActivity(new Intent(Main.this, Management.class));
            displayView(4);

            etat=false;
            mNotifCount=0;
            countnotif= String.valueOf(mNotifCount);
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putString(keynotifcount, countnotif);
            editor.commit();
        }
    });
   // return true;
    return super.onCreateOptionsMenu(menu);
}

这是我的菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/blue_pressed_want"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.nearby.pfe.MainActivity">
<item
    android:id="@+id/menu_notif"
    android:icon="@drawable/notification"
    android:actionLayout="@layout/view_feed_update_count"
    android:showAsAction="always"
    android:title="@string/notif"/>

这里的布局view_feed_update_count:

<?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="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right">
<!-- Menu Item Image -->
<ImageView
    android:layout_width="36dp"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:layout_alignParentRight="true"
    android:id="@+id/bell"
    android:src="@drawable/notification" />
<!-- Badge Count -->
<TextView
    android:id="@+id/actionbar_notifcation_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/counter_bg"
    android:layout_alignParentRight="true"
    android:visibility="invisible"
    android:paddingRight="13px"
    android:paddingLeft="13px"
    android:textColor="#ffffff" />
<TextView
    android:id="@+id/actionbar_notifcation_textviewoff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:padding="13px"
    android:layout_alignLeft="@+id/actionbar_notifcation_textview"
    android:layout_alignStart="@+id/actionbar_notifcation_textview" />

您尚未将项目中的 actionview 属性定义为相对布局。

相关内容

  • 没有找到相关文章

最新更新