mysql - cursor throwing nullpointer when called from content provider -
im trying query data 2 different tables using loadermanager in listfragment
. tables definetely have data, checked, reason query/cursor returning null. suspect syntax issue im not entirely sure. ran same query in mysql workbench, , worked. iv tried running query this:
cursor = db.query("customer, appointment", new string[]{"fullname", "physicaladdress", "date","time"}, "customer.id in(?)", new string[]{"select customerid appointment"},null,null,null);
and this:
cursor = db.rawquery("select fullname, physicaladdress, date, time customer, appointment customer.id in(select customerid appointment);", null);
but everytime line of code runs:
cursor.setnotificationuri(getcontext().getcontentresolver(), uri);
i nullpointer. in case means anything, im not using framelayout , dynamically adding listfragment. iv defined in activities xml file. appreciated. please let me know if iv left out.
databasecontentprovider:
package com.venon.nakomangsp; import android.content.contentprovider; import android.content.contentvalues; import android.content.urimatcher; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.net.uri; import android.support.annotation.nullable; @suppresswarnings("constantconditions") public class databasecontentprovider extends contentprovider { databasehelper dbhelper; private static final string provider_name = "com.venon.nakomangsp.databasecontentprovider"; private static final urimatcher urimatcher; public static final uri customer_uri = uri.parse("content://"+ provider_name+"/customer"); private static final int customer_code = 1; public static final uri appointment_uri = uri.parse("content://" + provider_name + "/appointment"); private static final int appointment_code = 2; public static final uri appointment_view_uri = uri.parse("content://" + provider_name + "/appointmentview"); private static final int appointment_view_code = 3; static{ urimatcher = new urimatcher(urimatcher.no_match); urimatcher.adduri(provider_name,"customer", customer_code); urimatcher.adduri(provider_name, "appointment", appointment_code); } @override public boolean oncreate() { dbhelper = new databasehelper(getcontext()); return true; } @nullable @override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { sqlitedatabase db = dbhelper.getwritabledatabase(); cursor cursor = null; switch(urimatcher.match(uri)){ case customer_code: cursor = db.query("customer", projection, selection, selectionargs, null, null, sortorder); break; case appointment_code: cursor = db.query("appointment", projection, selection, selectionargs, null, null, sortorder); break; case appointment_view_code://this case triggered //cursor = db.rawquery("select fullname, physicaladdress, date, time customer, appointment customer.id in(select customerid appointment);", null); cursor = db.query("customer, appointment", new string[]{"fullname", "physicaladdress", "date","time"}, "customer.id in(?)", new string[]{"select customerid appointment"},null,null,null); default: break; } cursor.setnotificationuri(getcontext().getcontentresolver(), uri); return cursor; } @nullable @override public uri insert(uri uri, contentvalues values) { sqlitedatabase db = dbhelper.getwritabledatabase(); switch(urimatcher.match(uri)){ case customer_code: db.insertorthrow("customer", null, values); break; case appointment_code: db.insertorthrow("appointment", null, values); break; default: break; } getcontext().getcontentresolver().notifychange(uri, null, false);//set true when syncadapter hooked return null; } @override public int delete(uri uri, string selection, string[] selectionargs) { sqlitedatabase db = dbhelper.getwritabledatabase(); switch(urimatcher.match(uri)){ case customer_code: db.delete("customer", selection, selectionargs); break; case appointment_code: db.delete("appointment", selection, selectionargs); break; default: break; } getcontext().getcontentresolver().notifychange(uri, null, false);//keep false when hooking syncadapter return 0; } @override public int update(uri uri, contentvalues values, string selection, string[] selectionargs) { sqlitedatabase db = dbhelper.getwritabledatabase(); switch(urimatcher.match(uri)){ case customer_code: db.update("customer", values, selection, selectionargs); break; case appointment_code: db.update("appointment", values, selection, selectionargs); break; default: break; } getcontext().getcontentresolver().notifychange(uri, null, false); return 0; } @nullable @override public string gettype(uri uri) { return null; } }
calling code:
package com.venon.nakomangsp; import android.content.intent; import android.database.cursor; import android.os.bundle; import android.support.v4.app.listfragment; import android.support.v4.app.loadermanager; import android.support.v4.content.cursorloader; import android.support.v4.content.loader; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.listview; import android.widget.simplecursoradapter; public class appointmentlistfragment extends listfragment implements loadermanager.loadercallbacks<cursor> { simplecursoradapter adapter; public static appointmentlistfragment newinstance() { appointmentlistfragment fragment = new appointmentlistfragment(); return fragment; } public appointmentlistfragment() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null) { // mparam1 = getarguments().getstring(arg_param1); // mparam2 = getarguments().getstring(arg_param2); } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_appointment_list, container, false); string[] = {"fullname", "physicaladdress", "date", "time"}; int[] = {r.id.customername_lv, r.id.physicaladdress_lv, r.id.date_lv, r.id.time_lv}; adapter = new simplecursoradapter(getcontext(), r.layout.appointment_list, null, from, to, 0); setlistadapter(adapter); getactivity().getsupportloadermanager().initloader(0, null, appointmentlistfragment.this); return view; } @override public void onlistitemclick(listview l, view v, int position, long id) { super.onlistitemclick(l, v, position, id); getactivity().startactivity(new intent(getcontext(), appointmentviewactivity.class)); } @override public loader<cursor> oncreateloader(int i, bundle bundle) { //this calling line return new cursorloader(getcontext(), databasecontentprovider.appointment_view_uri, null, null, null, null); } @override public void onloadfinished(loader<cursor> cursorloader, cursor cursor) { adapter.swapcursor(cursor); } @override public void onloaderreset(loader<cursor> cursorloader) { adapter.swapcursor(null); } }
if uri
not match 1 of 3 case elements, cursor null , npe:
switch(urimatcher.match(uri)){ case customer_code: cursor = db.query("customer", projection, selection, selectionargs, null, null, sortorder); break; case appointment_code: cursor = db.query("appointment", projection, selection, selectionargs, null, null, sortorder); break; case appointment_view_code://this case triggered //cursor = db.rawquery("select fullname, physicaladdress, date, time customer, appointment customer.id in(select customerid appointment);", null); cursor = db.query("customer, appointment", new string[]{"fullname", "physicaladdress", "date","time"}, "customer.id in(?)", new string[]{"select customerid appointment"},null,null,null); default: break; } cursor.setnotificationuri(getcontext().getcontentresolver(), uri);
the best way throw illegalargument exception in default
.
default: throw new illegalargumentexception("unknown uri");
Comments
Post a Comment