Note that there are some explanatory texts on larger screens.

plurals
  1. POError opening the database on a fragment
    primarykey
    data
    text
    <p>hi i'm self_studying Android and building a simple note app, i want to create, open and add simple data to database in a fragment when pressing a button but it doesn't work and according to error log: it seem the opening is the issue but i don't know why ( I've seen many questions on this but still can't figure out why ) my whitenote.java:</p> <pre><code>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; } </code></pre> <p>my database class:</p> <pre><code>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); } } </code></pre> <p>}</p> <p>and the error log: </p> <pre><code>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) </code></pre> <p>Thank you for any help in advance and sorry for my bad English</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload