I have successfully created a notification count on my action bar from AndrewS explanation in s/o.The count shows zero as i have not implemented any methods yet. On another activity i can show my sqlite row count with this method on my onCreate();:
int profile_counts = myDb.numberOfRows();
myDb.close();
txt.setText(String.valueOf(profile_counts));
I would like to duplicate the result here and show the count on my actionbar.So i tired this on my activity:
static Button notifCount;
static int mNotifCount = 0;
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.badge);
MenuItemCompat.setActionView(item, R.layout.feed_update_count);
View view = MenuItemCompat.getActionView(item);
int profile_counts = myDb.numberOfRows();
myDb.close();
notifCount = (Button)view.findViewById(R.id.notif_count);
notifCount.setText(String.valueOf(profile_counts));
}
return true;
}
private void setNotifCount(int count){
mNotifCount = count;
invalidateOptionsMenu();
}
But my activity crashes with the following logcat error.
05-07 06:12:48.690 3069-3069/com.snappy.stevekamau.cosmeticsapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4d8ab20)
05-07 06:12:48.690 3069-3069/com.snappy.stevekamau.cosmeticsapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.snappy.stevekamau.cosmeticsapp, PID: 3069
java.lang.NullPointerException
at com.snappy.stevekamau.cosmeticsapp.MainActivity.onCreateOptionsMenu(MainActivity.java:207)
at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
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:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Obviusly i'm not doing it right but i am stuck on this.Any help or suggestions will be appreciated.If you need more code,let me know.
On my database:
public int numberOfRows() {
String countQuery = "SELECT * FROM " + CONTACTS_TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int cnt = cursor.getCount();
cursor.close();
return cnt;
}