-2

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;
    }
Community
  • 1
  • 1
Steve Kamau
  • 2,755
  • 10
  • 42
  • 73

2 Answers2

1

As you can see in the stacktrace, you have a null object reference onCreateOptionsMenu, line 207 (check in your editor, or post the full file here to see exactly which line is that). Is the myDb object initialized somewhere, before onCreateOptionsMenu gets called?

Also:

  • You can probably make your counting method more efficient by using SELECT count(*) FROM ... (check the SQLite SQL documentation for more information)
  • You should add a try - catch - finally for making sure your cursor is closed always.
Sebastian
  • 1,076
  • 9
  • 24
0

Following both @user2539608 and @m vai i added the following line on my onCreate:

        myDb = new DBHelper(this);

Thanks alot guys.

Steve Kamau
  • 2,755
  • 10
  • 42
  • 73