我已经在AndrewS explanation in s/o的操作栏上成功创建了通知计数。计数显示为零,因为我还没有实现任何方法。 在另一个活动中,我可以在onCreate();:
上使用此方法显示我的sqlite行数
int profile_counts = myDb.numberOfRows();
myDb.close();
txt.setText(String.valueOf(profile_counts));
我想在此处复制结果并在我的操作栏上显示计数。所以我在我的活动上累了这个:
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();
}
但我的活动因以下logcat错误而崩溃。
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)
很明显我做得不对,但我坚持这个。任何帮助或建议将不胜感激。如果您需要更多代码,请告诉我。
在我的数据库中:
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;
}
答案 0 :(得分:1)
正如您在堆栈跟踪中看到的那样,您有一个空对象引用onCreateOptionsMenu,第207行(在您的编辑器中检查,或在此处发布完整文件以确切地查看该行)。在调用onCreateOptionsMenu之前,是否在某处初始化了myDb对象?
此外:
答案 1 :(得分:0)
在@ user2539608和@m vai之后我在onCreate上添加了以下行:
myDb = new DBHelper(this);
非常感谢你们。