我在我的应用程序中使用余数。我必须从数据库中获取事件并将详细信息设置到日历中,以便使用推送消息设置警报。我已经尝试了一些代码。
但我无法设置日历手册。获取空指针异常。我的代码在这里:
scheduleClient = new ScheduleClient(this);
scheduleClient.doBindService();
Calendar c = Calendar.getInstance();
String month = String.valueOf(c.get(Calendar.MONTH) + 1);
String year = String.valueOf(c.get(Calendar.YEAR));
String months = null;
if (month.startsWith("0") || month.startsWith("1")) {
months = year + "-" + month;
} else {
months = year + "-" + "0" + month;
}
if (isSetToTrue == true) {
LoadCalender(months);
// here set your checkBox to true
Toast.makeText(this, "Check box enabled", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(this, "Check box disabled", Toast.LENGTH_SHORT)
.show();
}
public void LoadCalender(String month) {
try {
Cursor cursor = null;
Database db = new Database(getApplicationContext());
cursor = db.getSelectedEventDetails(month);
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
do {
text = cursor.getString(cursor.getColumnIndex("event"));
title = "News/Events";
String dates = cursor.getString(cursor
.getColumnIndex("date"));
try {
DateFormat formatter;
Date date = new Date();
formatter = new SimpleDateFormat("yyyy-MM-dd");
date = (Date) formatter.parse(dates);
SimpleDateFormat fmt = new SimpleDateFormat(
"yyyy-MM-dd");
String todayStr = fmt.format(date);
java.sql.Date dt = java.sql.Date
.valueOf(new String(todayStr));
System.out.println("Today is " + dt);
String yr = String.valueOf(dt.getYear());
int year = Integer.parseInt("20"+yr.substring(1));
int months = dt.getMonth();
int day = dt.getDay();
Calendar c1 = Calendar.getInstance();
c1.set(year, months+1, day);
c1.set(Calendar.HOUR_OF_DAY, 0);
c1.set(Calendar.MINUTE, 0);
c1.set(Calendar.SECOND, 0);
// Ask our service to set an alarm for that date,
// this
// activity talks to the client that talks to the
// service
scheduleClient.setAlarmForNotification(c1);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (cursor.moveToNext());
}
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
// looping through All Contacts
}
Logcat 错误;
03-19 12:23:23.395: W/System.err(2984): java.lang.NullPointerException
03-19 12:23:23.405: W/System.err(2984): at com.rathnasoft.reminder.ScheduleClient.setAlarmForNotification(ScheduleClient.java:54)
03-19 12:23:23.426: W/System.err(2984): at com.rathnasoft.vikasa.HomeActivity.LoadCalender(HomeActivity.java:228)
03-19 12:23:23.446: W/System.err(2984): at com.rathnasoft.vikasa.HomeActivity.onCreate(HomeActivity.java:87)
03-19 12:23:23.466: W/System.err(2984): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-19 12:23:23.475: W/System.err(2984): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-19 12:23:23.506: W/System.err(2984): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-19 12:23:23.518: W/System.err(2984): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-19 12:23:23.525: W/System.err(2984): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-19 12:23:23.556: W/System.err(2984): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 12:23:23.577: W/System.err(2984): at android.os.Looper.loop(Looper.java:130)
03-19 12:23:23.586: W/System.err(2984): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-19 12:23:23.615: W/System.err(2984): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 12:23:23.635: W/System.err(2984): at java.lang.reflect.Method.invoke(Method.java:507)
03-19 12:23:23.645: W/System.err(2984): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-19 12:23:23.666: W/System.err(2984): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-19 12:23:23.676: W/System.err(2984): at dalvik.system.NativeStart.main(Native Method)