在片段上打开数据库时出错

时间:2013-12-07 07:47:33

标签: java android android-fragments

嗨我自我研究Android并构建一个简单的笔记应用程序,我想在按下按钮时创建,打开并向片段中的数据库添加简单数据但它不起作用并且根据错误日志:它似乎是开放是问题,但我不知道为什么(我已经看到很多问题,但仍然无法弄清楚为什么) 我的whitenote.java:

public class WhiteNote extends Fragment {
    private NoteDatabase note_database;
    private int i=0;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.white_note, container, false);

    note_database = new NoteDatabase(getActivity());

    final EditText text_ed_1;
    final EditText text_ed_2;
    Button button_addNote;
    Button button_listNote;
    Button button_delallNote;
    final TextView note_list;
    final LinearLayout note_container;

    text_ed_1 = (EditText)rootView.findViewById(R.id.textedit1);
    text_ed_2 = (EditText)rootView.findViewById(R.id.textedit2);
    button_addNote = (Button)rootView.findViewById(R.id.button1);
    button_listNote = (Button)rootView.findViewById(R.id.button2);
    button_delallNote = (Button)rootView.findViewById(R.id.button3);
    note_list=(TextView)rootView.findViewById(R.id.textview1);
    note_container = (LinearLayout)rootView.findViewById(R.id.listcontainer);

    button_addNote.setOnClickListener(new OnClickListener() {     
        //@Override
        public void onClick(View v) {
            note_database.open();
            note_database.createData("fragment homework" + i, "working with fragment");
            i++;
            note_database.close();
        }
    });
 /*
    button_addNote.setOnClickListener(new OnClickListener(){
        //@Override
        public void onClick(View v0) {
            note_database.open();
            LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addView = layoutInflater.inflate(R.layout.list_row, null);
            TextView textOut = (TextView)addView.findViewById(R.id.text_show1);
            textOut.setText(note_database.getData().toString());
            Button buttonRemove = (Button)addView.findViewById(R.id.text_delete1);
            buttonRemove.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v1) {
                    ((LinearLayout) addView.getParent()).removeView(addView);
                }
            });         
            note_container.addView(addView);
            note_database.close();
        }
    });
    */

    button_listNote.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            note_database.open();
            String ds = note_database.getData();
            note_database.close();
            note_list.setText(ds);
        }
    });

    button_delallNote.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            note_database.open();
            note_database.deleteAllNote();
            note_database.close();
        }
    });

    return rootView;
}

我的数据库类:

public class NoteDatabase {
private static final String DATABASE_NAME = "DB_NOTE";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NOTE = "NOTE";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TOPIC = "Topic: ";
public static final String COLUMN_NOTECONTENT = "Content: ";
public static final String COLUMN_NAME = "Name: ";

private static Context my_context;
static SQLiteDatabase note_db;
private OpenHelper noteopenHelper;

public NoteDatabase(Context c){
    NoteDatabase.my_context = c;
}

public NoteDatabase open() throws SQLException{
    noteopenHelper = new OpenHelper(my_context);
    note_db = noteopenHelper.getWritableDatabase();
    return this;
}

public void close(){
    noteopenHelper.close();
}

public long createData(String chude_note, String noidung_note) { 
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_TOPIC, chude_note);
    cv.put(COLUMN_NOTECONTENT, noidung_note);
    cv.put(COLUMN_NAME, "by K.An");
    return note_db.insert(TABLE_NOTE, null, cv);
}

public String getData() {
    String[] columns = new String[] {COLUMN_ID,COLUMN_TOPIC,COLUMN_NOTECONTENT,COLUMN_NAME};
    Cursor c = note_db.query(TABLE_NOTE, columns, null, null, null, null, null);
    /*if(c==null)
        Log.v("Cursor", "C is NULL");*/
    String result="";
    int iRow = c.getColumnIndex(COLUMN_ID);
    int iTopic = c.getColumnIndex(COLUMN_TOPIC);
    int iContent = c.getColumnIndex(COLUMN_NOTECONTENT);
    int iOwner = c.getColumnIndex(COLUMN_NAME);
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){     
        result = result +" "+ c.getString(iRow)
                + " - Topic: " + c.getString(iTopic)
                + " - Content: " + c.getString(iContent)
                + " - Owner: " + c.getString(iOwner) + "\n";
    }
    c.close();
    //Log.v("Result", result);
    return result;
}

public int deleteNote(String topic) {
    return note_db.delete(TABLE_NOTE, COLUMN_TOPIC + "='" + topic + "'", null);
}

public int deleteAllNote() {
    return note_db.delete(TABLE_NOTE, null, null);
}

//---------------- class OpenHelper ------------------
private static class OpenHelper extends SQLiteOpenHelper {
    public OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        arg0.execSQL("CREATE TABLE " + TABLE_NOTE + " ("
                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + COLUMN_TOPIC + " TEXT NOT NULL, "
                + COLUMN_NOTECONTENT + " TEXT NOT NULL, "
                + COLUMN_NAME + " TEXT NOT NULL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        arg0.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTE);
        onCreate(arg0);
    }
}

}

和错误日志:

12-07 13:18:55.188: E/AndroidRuntime(814): FATAL EXCEPTION: main
12-07 13:18:55.188: E/AndroidRuntime(814): java.lang.NullPointerException
12-07 13:18:55.188: E/AndroidRuntime(814):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
12-07 13:18:55.188: E/AndroidRuntime(814):  at     android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
12-07 13:18:55.188: E/AndroidRuntime(814):  at     java.lang.reflect.Method.invoke(Method.java:511)

感谢您提前提供任何帮助,抱歉我的英语不好

0 个答案:

没有答案