我想从我的数据库中读取一个文本列,然后更新它。 我在下面创建了两种方法:
public boolean editNote(Lesson lesson) {
ContentValues values = new ContentValues();
values.put(LessonsDBOpenHelper.COLUMN_NOTE, lesson.getNote());
String where = LessonsDBOpenHelper.COLUMN_ID + "=" + lesson.getId();
int result = database.update(LessonsDBOpenHelper.TABLE_LESSONS, values, where, null);
return (result == 1);
}
和
public String readNote(Lesson lesson) {
String query = "SELECT * FROM lessons " +
"WHERE lessonId =" +lesson.getId();
Cursor cursor = database.rawQuery(query, null);
String returnString = ""; // default value if nothing is found
if (cursor.moveToFirst()){
returnString = cursor.getString(cursor.getColumnIndex(LessonsDBOpenHelper.COLUMN_NOTE));
}
cursor.close();
return returnString;
}
然后我从另一个活动中调用editNote()和readNote()方法:
public class Note extends Activity {
private static final String LOGTAG = "MYAPP";
Lesson lesson;
LessonsDataSource datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_note);
Bundle b = getIntent().getExtras();
lesson = b.getParcelable(".model.Lesson");
Button noteSave = (Button) findViewById(R.id.note_save);
Button noteClear = (Button) findViewById(R.id.note_clear);
final TextView noteText = (TextView) findViewById(R.id.noteText);
String dbNote = datasource.readNote(lesson);
noteText.setText(dbNote);
Log.i(LOGTAG, "Note loaded from DB is "+dbNote);
noteClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noteText.setText("");
lesson.setNote("");
datasource.editNote(lesson);
}
});
noteSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String note = noteText.getText().toString();
lesson.setNote(note);
datasource.editNote(lesson);
Log.i(LOGTAG, "Note text changed to "+note);
String dbNote = datasource.readNote(lesson);
Log.i(LOGTAG, "Note text in DB is "+dbNote);
}
});
}
}
但是当我的应用程序获得readNote或editNote方法时崩溃了。 我做错了什么?
我只想阅读并更新我的数据库中的文本。
修改 这是LogCat:
01-30 02:47:29.809: D/AndroidRuntime(30850): Shutting down VM
01-30 02:47:29.809: W/dalvikvm(30850): threadid=1: thread exiting with uncaught exception (group=0x41744ba8)
01-30 02:47:29.809: E/AndroidRuntime(30850): FATAL EXCEPTION: main
01-30 02:47:29.809: E/AndroidRuntime(30850): Process: com.me.linux, PID: 30850
01-30 02:47:29.809: E/AndroidRuntime(30850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.linux/com.me.linux.Note}: java.lang.NullPointerException
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.os.Handler.dispatchMessage(Handler.java:102)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.os.Looper.loop(Looper.java:136)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-30 02:47:29.809: E/AndroidRuntime(30850): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 02:47:29.809: E/AndroidRuntime(30850): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 02:47:29.809: E/AndroidRuntime(30850): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-30 02:47:29.809: E/AndroidRuntime(30850): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-30 02:47:29.809: E/AndroidRuntime(30850): at dalvik.system.NativeStart.main(Native Method)
01-30 02:47:29.809: E/AndroidRuntime(30850): Caused by: java.lang.NullPointerException
01-30 02:47:29.809: E/AndroidRuntime(30850): at com.me.linux.Note.onCreate(Note.java:31)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.Activity.performCreate(Activity.java:5231)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-30 02:47:29.809: E/AndroidRuntime(30850): ... 11 more
01-30 02:59:51.589: I/MYAPP(31030): Database closed
编辑2: 以下是启动此活动的意图的putExtra代码:
case R.id.action_edit_note:
Intent intent = new Intent(this, Note.class);
intent.putExtra(".model.Lesson", lesson);
startActivityForResult(intent, 1002);
break;
答案 0 :(得分:1)
您必须使用readNote()
方法重新打开数据库,以便创建查询。