1

我正在尝试制作一个从数据库中填充的列表视图。在数据库中,我使用与列表相同的活动中的警报对话框输入数据。我在警报对话框中有 3 个按钮:保存、删除、关闭。我的问题是,当我单击保存按钮时,它给了我一个错误。

这是我的 Liste.class (列表在哪里的活动):

private View.OnClickListener onAdd = new View.OnClickListener() {

    public void onClick(View v) {

        edit_box();
    }
};
private AdapterView.OnItemClickListener onListClick = new  AdapterView.OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {

        noteId = String.valueOf(id);
        edit_box();
    }   
};
private void edit_box(){

    final View addView = getLayoutInflater().inflate(R.layout.add, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(addView);
    EditText editNote=(EditText) addView.findViewById(R.id.title);
    if(noteId != null)
    {
        Cursor c = helper.getById(noteId);
        c.moveToFirst();
        editNote.setText(helper.getNote(c));
    }
    builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton){

                    EditText editNote = (EditText) addView.findViewById(R.id.title);
            if(noteId==null){
                helper.insert(editNote.getText().toString());
                noteId = null;
            }
            dataset_cursor.requery();
        }           
        });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

        public void onClick(DialogInterface dialog, int whichButton){

        }
    });
    builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {

            if(noteId == null)
            {
                return;
            }
            else{
                helper.delete(noteId);
                noteId=null;
            }
            dataset_cursor.requery();
        }            
    });
    builder.show();
}

class NoteAdapter extends CursorAdapter{

        NoteAdapter(Cursor c){
        super(Liste.this, c);
    }

    @Override
    public void bindView(View row, Context ctxt, Cursor c) {

        NoteHolder holder = (NoteHolder) row.getTag();
        holder.populateFrom(c, helper);
    }
    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {

        LayoutInflater inflater=getLayoutInflater();
        View row =inflater.inflate(R.layout.row, parent, false);
        NoteHolder holder = new NoteHolder(row);

        row.setTag(holder);

        return(row);
    }}
    static class NoteHolder {

            private TextView note = null;
        NoteHolder(View row) {
            note=(TextView)row.findViewById(R.id.note);
        }
        void populateFrom(Cursor c, DBhelp helper){
            note.setText(helper.getNote(c));
        }
    }

这是我的 DBHelper 课程的一部分:

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("CREATE TABLE Notes(_id INTEGER PRIMARY KEY AUTOINCREMENT, note TEXT);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void insert(String note){

            ContentValues cv = new ContentValues();
    cv.put("note", note);
    getWritableDatabase().insert("Notes", "note", cv);
}
public void update(String id, String note) {

    ContentValues cv = new ContentValues();
    String[] args={id};
    cv.put("note", note);
    getWritableDatabase().update("Notes", cv, "_id=?", args);
}
public void delete(String id){

    getWritableDatabase().delete("Notes", "_id=?", new String[] {id});
}
public Cursor getAll(){

    return(getReadableDatabase()
            .rawQuery("SELECT _id, note FROM Notes", null));
}
public Cursor getById(String id){

    String[] args={id};
    return(getReadableDatabase()
            .rawQuery("SELECT _id, note FROM Notes WHERE _id=?",
                                args));
}
public String getNote(Cursor c){

    return(c.getString(1));
}
  }

我在问你们大家是否能说出可能是什么问题。我真诚地验证了所有代码,但我无法弄清楚我写错的地方。如果您能给我一个建议,我将不胜感激,或者更好的是,您可以告诉我问题出在哪里。谢谢 !

PS:这是logcat:

12-30 12:57:59.079: W/KeyCharacterMap(340): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-30 12:58:00.359: D/AndroidRuntime(340): Shutting down VM
12-30 12:58:00.359: W/dalvikvm(340): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-30 12:58:00.390: E/AndroidRuntime(340): FATAL EXCEPTION: main
12-30 12:58:00.390: E/AndroidRuntime(340): java.lang.NullPointerException
12-30 12:58:00.390: E/AndroidRuntime(340):  at com.project.mapshop.Liste$3.onClick(Liste.java:96)
12-30 12:58:00.390: E/AndroidRuntime(340):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
12-30 12:58:00.390: E/AndroidRuntime(340):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 12:58:00.390: E/AndroidRuntime(340):  at android.os.Looper.loop(Looper.java:123)
12-30 12:58:00.390: E/AndroidRuntime(340):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-30 12:58:00.390: E/AndroidRuntime(340):  at java.lang.reflect.Method.invokeNative(Native Method)
12-30 12:58:00.390: E/AndroidRuntime(340):  at java.lang.reflect.Method.invoke(Method.java:521)
12-30 12:58:00.390: E/AndroidRuntime(340):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-30 12:58:00.390: E/AndroidRuntime(340):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-30 12:58:00.390: E/AndroidRuntime(340):  at dalvik.system.NativeStart.main(Native Method)
4

0 回答 0