Android, context not found in database construction -
so trying use database fill listview within fragment. far point trying insert data empty table, whenever run insert method app crashes. far can tell, error nullpointerexception when database being created, caused context not being returned properly. however, don't see what's wrong context. try include relevant classes.
contract class
public final class notescontract { //empty constructor public notescontract(){} /* inner class defines table contents */ public static abstract class feedentry implements basecolumns { public static final string table_name = "notes"; public static final string column_name_title = "title"; public static final string column_name_body = "body"; //add more columns database here } } sqliteopenhelper
public class mysqlitehelper extends sqliteopenhelper { private static final string text_type = " text"; private static final string int_type = " integer"; private static final string comma_sep = ","; private static final string sql_create_entries = "create table " + feedentry.table_name + " (" + feedentry._id + " integer primary key," + feedentry.column_name_title + text_type + comma_sep + feedentry.column_name_body + text_type + " )"; private static final string sql_delete_entries = "drop table if exists " + feedentry.table_name; // if change database schema, must increment database version. public static final int database_version = 1; public static final string database_name = "feedreader.db"; public mysqlitehelper(context context) { super(context, database_name, null, database_version); } public void oncreate(sqlitedatabase db) { db.execsql(sql_create_entries); } public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // database cache online data, upgrade policy // discard data , start on db.execsql(sql_delete_entries); oncreate(db); } public void ondowngrade(sqlitedatabase db, int oldversion, int newversion) { onupgrade(db, oldversion, newversion); } } mainactivityfragment
public class mainactivityfragment extends fragment { //databse instatiation mysqlitehelper mdbhelper = new mysqlitehelper(getactivity()); //other stuff @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { //primary view inflator final view rootview = inflater.inflate(r.layout.fragment_main, container, false); //other stuff final imagebutton button2 = (imagebutton) rootview.findviewbyid(r.id.addnotebutton); button2.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if((notetitle.gettext().tostring().trim()).equals("")){ toast.maketext(getactivity().getbasecontext(), "no title specified", toast.length_short).show(); }else { string notetitlestring = notetitle.gettext().tostring(); string notebodystring = notebody.gettext().tostring(); toast.maketext(getactivity().getbasecontext(), "added: " + notetitlestring, toast.length_short).show(); //clear text fields , hide keyboard notetitle.gettext().clear(); notebody.gettext().clear(); final inputmethodmanager imm = (inputmethodmanager) getactivity().getsystemservice(context.input_method_service); imm.hidesoftinputfromwindow(getview().getwindowtoken(), 0); addnotes(notetitlestring, notebodystring); } } }); } private void addnotes(string title, string body){ // gets data repository in write mode sqlitedatabase db = mdbhelper.getwritabledatabase(); // create new map of values, column names keys contentvalues values = new contentvalues(); values.put(feedentry.column_name_title, title); values.put(feedentry.column_name_body, body); // insert new row, returning primary key value of new row long newrowid; newrowid = db.insert( feedentry.table_name, null, values); db.close(); } error stack
process: com.example.ggould.scribble, pid: 21011 java.lang.nullpointerexception: attempt invoke virtual method 'android.database.sqlite.sqlitedatabase android.content.context.openorcreatedatabase(java.lang.string, int, android.database.sqlite.sqlitedatabase$cursorfactory, android.database.databaseerrorhandler)' on null object reference @ android.database.sqlite.sqliteopenhelper.getdatabaselocked(sqliteopenhelper.java:223) @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:163) @ com.example.ggould.scribble.mainactivityfragment.addnotes(mainactivityfragment.java:228) @ com.example.ggould.scribble.mainactivityfragment.access$000(mainactivityfragment.java:26) @ com.example.ggould.scribble.mainactivityfragment$2.onclick(mainactivityfragment.java:136) @ android.view.view.performclick(view.java:4756) @ android.view.view$performclick.run(view.java:19749) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
when calling mysqlitehelper mdbhelper = new mysqlitehelper(getactivity()); fragment not yet attached activity, getactivity() returns null. try creating mysqlitehelper object in oncreateview method.
Comments
Post a Comment