android - How to use Content Resolver to query SQLite database? -


currently i'm using db.query() method access database directly,and code running perfectly. when try use content resolver access database using cursor cursor = getcontentresolver().query() method app isn't starts.i have created contract class store constant , customerprovider class use contantprovider.

catalogactivity (mainactivity):

public class catalogactivity extends appcompatactivity { private customerdbhelper mdbhelper; button addbutton;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_catalog);      addbutton= (button) findviewbyid(r.id.addbutton);     addbutton.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             intent intent = new intent(catalogactivity.this, addcustomeractivity.class);             startactivity(intent);         }     });     mdbhelper=new customerdbhelper(this);     displaydatabaseinfo();  }  @override protected void onstart() {     super.onstart();     displaydatabaseinfo(); }  protected void displaydatabaseinfo(){     customerdbhelper mdbhelper=new customerdbhelper(this);     sqlitedatabase db=mdbhelper.getreadabledatabase();      string[] projection={customerentry._id,             customerentry.column_customer_name,             customerentry.column_customer_address,             customerentry.column_customer_phone };       // cursor cursor = db.query(customerentry.table_name,projection,null,null,null,null,null);//when use command code runs     cursor cursor = getcontentresolver().query(             customerentry.content_uri,             projection,             null,             null,             null); // want use method here problem occur,app doesn't stars.       try {         textview  tv1 = (textview) findviewbyid(r.id.tv1);         tv1.settext("number of rows= " + cursor.getcount());         tv1.append("\n"+"currentid-currentname-currentaddress-cureentphone");         int idcolumnindex=cursor.getcolumnindex(customerentry._id);         int namecolumnindex = cursor.getcolumnindex(customerentry.column_customer_name);         int addresscolumnindex = cursor.getcolumnindex(customerentry.column_customer_address);         int phonecolumnindex = cursor.getcolumnindex(customerentry.column_customer_phone);          while(cursor.movetonext()){              int  currentid=cursor.getint(idcolumnindex);              string currentname = cursor.getstring(namecolumnindex);              string currentaddress = cursor.getstring(addresscolumnindex);              int currentphone = cursor.getint(phonecolumnindex);              tv1.append("\n"+currentid+"-"+currentname+"-"+currentaddress+"-"+currentphone);          }      }     {         cursor.close();     } } 

cutomercontract (contract class):

public final class customercontract { private customercontract() { }  public static final string content_authority = "com.example.android.try1"; public static final uri base_content_authority = uri.parse("content://" + content_authority); public static final string path_customer_table = "customer";  public static final class customerentry implements basecolumns {      public static final uri content_uri = uri.withappendedpath(base_content_authority, path_customer_table);      public final static string table_name = "customer";     public final static string _id = basecolumns._id;     public final static string column_customer_name = "name";     public final static string column_customer_address = "address";     public final static string column_customer_phone = "phone"; }  }     

customerprovider (contentprovider) class

public class customerprovider extends contentprovider { private customerdbhelper mdbhelper; private static final int customer = 100; private static final int customer_id =101; private static final urimatcher surimatcher = new urimatcher(urimatcher.no_match); static {     surimatcher.adduri(customercontract.content_authority,customercontract.path_customer_table,customer);     surimatcher.adduri(customercontract.content_authority,customercontract.path_customer_table  + "/#",customer_id); }  @override public boolean oncreate() {     mdbhelper=new customerdbhelper(getcontext());     return true; }  @nullable @override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs,                     string sortorder) {     sqlitedatabase database = mdbhelper.getreadabledatabase();     cursor cursor;     int match = surimatcher.match(uri);     switch (match){         case customer:             cursor = database.query(customerentry.table_name, projection, selection, selectionargs,                     null, null, sortorder);             break;          case customer_id:             selection = customerentry._id + "=?";             selectionargs = new string[]{string.valueof(contenturis.parseid(uri))};             cursor = database.query(customerentry.table_name, projection,selection,selectionargs,null,null,sortorder);             break;          default:             throw new illegalargumentexception("cannot query unknown uri " + uri);     }     return cursor; }  @nullable @override public string gettype(@nonnull uri uri) {     return null; }  @nullable @override public uri insert(@nonnull uri uri, @nullable contentvalues contentvalues) {     return null; }  @override public int delete(@nonnull uri uri, @nullable string s, @nullable string[] strings) {     return 0; }  @override public int update(@nonnull uri uri, @nullable contentvalues contentvalues, @nullable string s, @nullable string[] strings) {     return 0; } } 

logcat showing following message:

09-15 02:55:02.768 3391-3391/com.example.android.try1 e/activitythread: failed find provider info com.example.android.try1 

09-15 02:55:02.768 3391-3391/com.example.android.try1 d/androidruntime: shutting down vm 09-15 02:55:02.769 3391-3391/com.example.android.try1 e/androidruntime: fatal exception: main process: com.example.android.try1, pid: 3391 java.lang.runtimeexception: unable start activity componentinfo{com.example.android.try1/com.example.android.try1.catalogactivity}: java.lang.nullpointerexception: attempt invoke interface method 'void android.database.cursor.close()' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2416) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476) @ android.app.activitythread.-wrap11(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1344) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.nullpointerexception: attempt invoke interface method 'void android.database.cursor.close()' on null object reference @ com.example.android.try1.catalogactivity.displaydatabaseinfo(catalogactivity.java:91) @ com.example.android.try1.catalogactivity.oncreate(catalogactivity.java:40) @ android.app.activity.performcreate(activity.java:6237) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1107) @ android.app.activitythread.performlaunchactivity(activitythread.java:2369) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476)  @ android.app.activitythread.-wrap11(activitythread.java)  @ android.app.activitythread$h.handlemessage(activitythread.java:1344)  @ android.os.handler.dispatchmessage(handler.java:102)  @ android.os.looper.loop(looper.java:148)  @ android.app.activitythread.main(activitythread.java:5417)  @ java.lang.reflect.method.invoke(native method)  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726)  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) 


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -