在我的数据库中存储名称,注释和日期时间字段(YYYY-MM-DD HH:MM)。当我尝试使用select语句检索数据时,它不起作用并显示空指针异常,用于创建和插入数据库的代码是
SQLiteDatabase database;
String st = yeartopass+"-"+monthtopass+"-"+daytopass+" "+hourtopass+":"+minutetopass;
database = getApplicationContext().openOrCreateDatabase(
"universalreminder", MODE_PRIVATE, null);
database.execSQL("create table if not exists birthdaytable(nameofperson varchar(20),comment varchar(150),bdatetime varchar(20));");
database.execSQL("insert into birthdaytable values('"+name+"','"+comment+"','"+st+"');");
Toast.makeText(getApplicationContext(),st,Toast.LENGTH_LONG).show();
这是我尝试选择的代码
public class CustomDialog extends Activity{
SQLiteDatabase database;
TextView text;
Calendar cal = Calendar.getInstance();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// database.execSQL("select bdatetime from birthdaytable ");
// Create custom dialog object
final Dialog dialog = new Dialog(CustomDialog.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialogwindow);
// Set dialog title
dialog.setTitle("Custom Dialog");
getname();
// set values for custom dialog components - text, image and button
text = (TextView) dialog.findViewById(R.id.textView1indialog);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.buttonindialog);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
}
public void getname(){
final String TAG = "CustomDialog";
int year, month, day, hour, minute;
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);
hour = cal.get(Calendar.HOUR_OF_DAY);
minute = cal.get(Calendar.MINUTE);
String datetimecheck = year+"-"+month+"-"+day+" "+hour+":"+minute;
try{
database = getApplicationContext().openOrCreateDatabase(
"universalreminder", MODE_PRIVATE, null);
Toast.makeText(getApplicationContext(), datetimecheck+" ", Toast.LENGTH_LONG).show();
Cursor cursor = database.rawQuery("select nameofperson from birthdaytable where bdatetime = '"+datetimecheck+"';",null);
// where bdatetime = '" + datetimecheck+"'",null );
//("birthdaytable", new String[] {"nameofperson"}, "bdatetime = '" + datetimecheck+"'", null, null, null, null);
if(cursor.moveToNext()){
text.setText(cursor.getString(0));
}
}catch(Exception e){
Log.e(TAG, "", e);
Toast.makeText(getApplicationContext(),e+ " ", Toast.LENGTH_LONG).show();
}
}
}
当我打印异常时,它说java.lang.NullPoninterException
我的日志cat条目是
04-20 11:29:26.690: E/CustomDialog(1142): java.lang.NullPointerException
04-20 11:29:26.690: E/CustomDialog(1142): at com.universalremainder.CustomDialog.getname(CustomDialog.java:71)
04-20 11:29:26.690: E/CustomDialog(1142): at com.universalremainder.CustomDialog.onCreate(CustomDialog.java:34)
04-20 11:29:26.690: E/CustomDialog(1142): at android.app.Activity.performCreate(Activity.java:5243)
04-20 11:29:26.690: E/CustomDialog(1142): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-20 11:29:26.690: E/CustomDialog(1142): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
04-20 11:29:26.690: E/CustomDialog(1142): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-20 11:29:26.690: E/CustomDialog(1142): at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-20 11:29:26.690: E/CustomDialog(1142): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-20 11:29:26.690: E/CustomDialog(1142): at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 11:29:26.690: E/CustomDialog(1142): at android.os.Looper.loop(Looper.java:137)
04-20 11:29:26.690: E/CustomDialog(1142): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-20 11:29:26.690: E/CustomDialog(1142): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 11:29:26.690: E/CustomDialog(1142): at java.lang.reflect.Method.invoke(Method.java:515)
04-20 11:29:26.690: E/CustomDialog(1142): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-20 11:29:26.690: E/CustomDialog(1142): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-20 11:29:26.690: E/CustomDialog(1142): at dalvik.system.NativeStart.main(Native Method)
这是第71行的遗嘱
text.setText(cursor.getString(0));
和第34行
getname();
答案 0 :(得分:0)
这是第71行的遗嘱
text.setText(cursor.getString(0));
text
是null
。
没有足够的上下文来帮助您准确地初始化它,但可能类似于
text = (TextView)dialog.findViewById(R.id.some_textview_id);
诀窍。