android studio add markers to google maps from sqlite database -
in android app i'm trying add several markers sqlite mapsactivity. created do-while cycle getting cities in sqlite table , convert cities in coordinates (lat, long), there's in error. can me?
---mapsactivity.java---
public class mapsactivity extends fragmentactivity implements onmapreadycallback { private googlemap mmap; databasehelper mydb; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_maps); // obtain supportmapfragment , notified when map ready used. supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this); } @override public void onmapready(googlemap googlemap) { mmap = googlemap; // add marker in sydney , move camera latlng sydney = new latlng(-34, 151); latlng ruvo = new latlng(-45, 123); mmap.addmarker(new markeroptions().position(sydney).title("marker in sydney")); mmap.addmarker(new markeroptions().position(ruvo).title("marker in sydney")); mmap.movecamera(cameraupdatefactory.newlatlng(ruvo)); displaycities(); } public void onsearch(view view) { edittext textcity = (edittext) findviewbyid(r.id.textcity); string location = textcity.gettext().tostring(); list<android.location.address> addresslist = null; if (location != null) { geocoder geocoder = new geocoder(this); try { addresslist = geocoder.getfromlocationname(location, 1); } catch (ioexception e) { e.printstacktrace(); } android.location.address address = addresslist.get(0); latlng latlng = new latlng(address.getlatitude(), address.getlongitude()); mmap.addmarker(new markeroptions().position(latlng).title("marker pointed")); mmap.animatecamera(cameraupdatefactory.newlatlng(latlng)); } } public void displaycities() { list<android.location.address> addresslist = null; cursor database = mydb.display(); if (database.getcount()== 0) { toast.maketext(this, "nothing found", toast.length_long).show(); return; } //string array of cities database.movetofirst(); string[] cities = new string[database.getcount()]; int n = 0; { cities[n] = database.getstring(3).tostring(); n=n+1; } while (database.movetonext()); geocoder geocoder = new geocoder(this); try { addresslist = geocoder.getfromlocationname(cities[n], 1); } catch (ioexception e) { e.printstacktrace(); } android.location.address address = addresslist.get(0); latlng latlng = new latlng(address.getlatitude(), address.getlongitude()); mmap.addmarker(new markeroptions().position(latlng).title("marker pointed")); } }
---databasehelper.java---
public class databasehelper extends sqliteopenhelper { private static final int database_version = 1; private static final string database_name = "student.db"; public static final string table_name = "student_table"; //very important: cursoradapter needs "_id" column, written here public static final string col_1 = "_id"; public static final string col_2 = "name"; public static final string col_3 = "surname"; public static final string col_4 = "city"; public arraylist<string> results = new arraylist<>(); public databasehelper(context context, string name, sqlitedatabase.cursorfactory factory, int version) { super(context, database_name, factory, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table " + table_name + " ( " + col_1 + " integer primary key autoincrement , " + col_2 + " text , " + col_3 + " text , " + col_4 + " text );"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists" + table_name); oncreate(db); } public cursor selected(long id) { sqlitedatabase db = this.getwritabledatabase(); string[] columns = new string[]{databasehelper.col_1, databasehelper.col_2, databasehelper.col_3, databasehelper.col_4}; cursor c = db.query(databasehelper.table_name, columns, databasehelper.col_1 + "=" + id, null, null, null, null); if (c != null) { c.movetofirst(); } return c; } public boolean insertdata(string name, string surname, string city) { sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(col_2, name); contentvalues.put(col_3, surname); contentvalues.put(col_4, city); long result = db.insert(table_name, null, contentvalues); if (result == -1) return false; else return true; } //method select query public cursor display() { sqlitedatabase db = this.getreadabledatabase(); cursor res = db.rawquery("select * " + table_name, null); return res; } public boolean updatedata(string id, string name, string surname, string city) { sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(col_1, id); contentvalues.put(col_2, name); contentvalues.put(col_3, surname); contentvalues.put(col_4, city); db.update(table_name, contentvalues, "_id = ?", new string[] {id}); return true; } public integer deletedata (string id) { sqlitedatabase db = this.getwritabledatabase(); return db.delete(table_name, "_id = ?", new string[] {id}); } }
edit: add error in logcat
beginning of crash
06-28 12:17:13.332 2915-2915/com.example.android.sqlite e/androidruntime: fatal exception: main process: com.example.android.sqlite, pid: 2915 java.lang.nullpointerexception: attempt invoke virtual method 'android.database.cursor com.example.android.sqlite.databasehelper.display()' on null object reference
at com.example.android.sqlite.mapsactivity.displaycities(mapsactivity.java:76)
@ com.example.android.sqlite.mapsactivity.onmapready(mapsactivity.java:51)
@ com.google.android.gms.maps.supportmapfragment$zza$1.zza(unknown source)
@ com.google.android.gms.maps.internal.zzo$zza.ontransact(unknown source)
@ android.os.binder.transact(binder.java:387)
@ com.google.android.gms.maps.internal.be.a(sourcefile:82)
@ com.google.maps.api.android.lib6.e.fb.run(unknown source)
@ android.os.handler.handlecallback(handler.java:739)
@ android.os.handler.dispatchmessage(handler.java:95)
@ 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)
06-28 12:17:15.410 2915-2915/com.example.android.sqlite i/process: sending signal. pid: 2915 sig: 9
Comments
Post a Comment