我们希望使用存储在我们的表事件中的游标来获取数据,表格的字段是日期,开始时间和结束时间。
我们希望从当前日期=日期和当前时间=开始时间的表开始时间,但我们无法获取数据。
我们的数据库获取方法如下......
public String fetch1(String da,String ti)
{
SQLiteDatabase db =this.getReadableDatabase();
Cursor c2=db.query(TABLE_eve,new String[]{ KEY_from},KEY_fr_date + "=? AND "+KEY_from + "=?",new String[] {String.valueOf(da),String.valueOf(ti)},null,null,null,null);
if(c2!=null)
s= c2.getString(0);
c2.close();
db.close();
return s;
}
我们在活动中调用fetch方法,代码如下
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
java.util.Date date = new java.util.Date();
String datetime = dateFormat.format(date);
Toast_msg="Exxxx"+datetime;
Show_Toast(Toast_msg);
Calendar cal=Calendar.getInstance();
cal.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");
String cTime=sdf.format(cal.getTime());
Toast_msg="Exxxx"+cTime;
Show_Toast(Toast_msg);
//********** code for fetching data from database*****************//
t1= dh.fetch1(datetime,cTime);
try
{
String splitTime[]=t1.split(":");
String hours=splitTime[0];
String minutes=splitTime[1];
hr=Integer.parseInt(hours);
min=Integer.parseInt(minutes);
}
catch(Exception e)
{
Toast_msg="Exn";
Show_Toast(Toast_msg);
}
我们的代码抛出空指针异常,logcat文件是
04-18 09:19:50.760: D/AndroidRuntime(1122): Shutting down VM
04-18 09:19:50.760: W/dalvikvm(1122): threadid=1: thread exiting with uncaught exception (group=0xb4a1eb90)
04-18 09:19:50.770: E/AndroidRuntime(1122): FATAL EXCEPTION: main
04-18 09:19:50.770: E/AndroidRuntime(1122): Process: com.event.android, PID: 1122
04-18 09:19:50.770: E/AndroidRuntime(1122): java.lang.NullPointerException
04-18 09:19:50.770: E/AndroidRuntime(1122): at com.event.android.AddPg$9.onClick(AddPg.java:243)
04-18 09:19:50.770: E/AndroidRuntime(1122): at android.view.View.performClick(View.java:4424)
04-18 09:19:50.770: E/AndroidRuntime(1122): at android.view.View$PerformClick.run(View.java:18383)
04-18 09:19:50.770: E/AndroidRuntime(1122): at android.os.Handler.handleCallback(Handler.java:733)
04-18 09:19:50.770: E/AndroidRuntime(1122): at android.os.Handler.dispatchMessage(Handler.java:95)
04-18 09:19:50.770: E/AndroidRuntime(1122): at android.os.Looper.loop(Looper.java:137)
04-18 09:19:50.770: E/AndroidRuntime(1122): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-18 09:19:50.770: E/AndroidRuntime(1122): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 09:19:50.770: E/AndroidRuntime(1122): at java.lang.reflect.Method.invoke(Method.java:515)
04-18 09:19:50.770: E/AndroidRuntime(1122): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-18 09:19:50.770: E/AndroidRuntime(1122): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-18 09:19:50.770: E/AndroidRuntime(1122): at dalvik.system.NativeStart.main(Native Method)
04-18 09:19:53.670: I/Process(1122): Sending signal. PID: 1122 SIG: 9
04-18 09:19:55.670: I/Choreographer(1161): Skipped 66 frames! The application may be doing too much work on its main thread.
04-18 09:19:55.720: D/gralloc_goldfish(1161): Emulator without GPU emulation detected.
请建议我们如何处理数据,你可以建议你自己的代码。我们已经在这段代码上做了很多工作,但是我们无法获取
答案 0 :(得分:0)
这取决于列存储的数据类型,如果您将其存储为日期时间类型,则需要使用正确的格式执行查询,您可以在此帖子中查看更多内容:SQLite, making a query based on Dates