android - How to get the Context of an Application in a Fragment -
first have error message
caused by: 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) @ net.purplebug.mydoctorfinder.mydbhandler.showdoctors(mydbhandler.java:133) @ net.purplebug.mydoctorfinder.homefragment.showdoctorlist(homefragment.java:131)
so thought way wrote context parameter dbhandler instance.
this code triggers error. it's method in class extends fragment.
public void showdoctorlist() { string result = "match found"; mydbhandler dbhandler = new mydbhandler(getcontext(), null, null, 1); arraylist<arraylist<string>> docarray = new arraylist<arraylist<string>>(); doctordatamodel doctor = null; try { doctor = dbhandler.showdoctors(); } catch (sqlexception e) { e.printstacktrace(); } if (doctor != null) { tablerow row = new tablerow(getactivity()); row.setlayoutparams(new layoutparams(layoutparams.match_parent, layoutparams.wrap_content)); docarray = doctor.getdocarray(); for(int = 0; < docarray.size(); i++) { for(int j = 0; j < docarray.get(i).size(); j++) { textview textview = new textview(getactivity()); textview.setlayoutparams(new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content)); textview.setpadding(5, 5, 5, 5); textview.settext(docarray.get(i).get(j) + " "); row.addview(textview); } tablelayout.addview(row); } } else { result = "no match found"; } log.d("query result", result); }
and code in dbhandler in relation error.
public mydbhandler(context context, string name, sqlitedatabase.cursorfactory factory, int version) { super(context, database_name, factory, database_version); this.mycontext = context; } public doctordatamodel showdoctors() throws sqlexception { int index = 0; string doclastname, docfirstname, docid; string query = "select doctor_id, lastname, firstname " + table_doctor; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(query, null); doctordatamodel doctor = new doctordatamodel(); while (cursor.movetonext()) { docid = cursor.getstring(0); doclastname = cursor.getstring(1); docfirstname = cursor.getstring(2); doctor.setdocarraylist(index, docid, doclastname, docfirstname); index++; } index = 0; cursor.close(); db.close(); return doctor; }
i think it's way context in code.
mydbhandler dbhandler = new mydbhandler(getcontext(), null, null, 1);
or else problem. guys think?
you can define global variable:
private context context;
and in oncreate method, initialize :
context = this.getactivity();
later use following avoid
nullpointerexception
:
if(context!=null){ //your code }
and can use context
variable in fragment functions/methods.
Comments
Post a Comment