android - NullPointerExeption in SimpleCursor adapter.Works with onLocationChanged() -
i hava database places , coordinates. want create fragment, hava list of places more 5 kilometrs me. code is:
i start fragment next:
case r.id.btn_near: if(isonline()){ ft = getsupportfragmentmanager().begintransaction().replace( r.id.content_frame, frg); ft.commit(); } else toast.maketext(this, "Подключите интернет", 1000).show(); break;
fragmentnear.class
public class fragmentnear extends fragment implements android.location.locationlistener { cursor c; view v; listview lvmore5; database db; protected locationmanager locationmanager; string lat; string provider; protected string latitude, longitude; protected boolean gps_enabled, network_enabled; double mylat, mylong; simplecursoradapter scadapter; string[] from; int[] to; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.d("mylogs", "-------- oncreateview " + mylat + " " + mylong); v = inflater.inflate(r.layout.fragment_near, null); lvmore5 = (listview) v.findviewbyid(r.id.lv_more_5); log.d("mylogs", "-------- oncreateviewend " + mylat + " " + mylong); return v; } @override public void onresume() { db = new database(getactivity()); db.open(); = new string[] { database.place_name, database.place_distance, database.place_adress }; = new int[] { r.id.tv_place_name_n, r.id.tv_distance, r.id.tv_place_adress_n }; log.d("mylogs", "-------- oncreate " + mylat + " " + mylong); locationmanager = (locationmanager) getactivity().getsystemservice( context.location_service); locationmanager.requestlocationupdates( locationmanager.network_provider, 0, 0, this); super.onresume(); } public double getdistance(double mylat, double mylong, double pointlat, double pointlong) { log.d("mylogs", "getdistance =" + mylat + " " + mylong); double distance = 0; location locationa = new location("a"); // 69.019207,33.085015", locationa.setlatitude(mylat); locationa.setlongitude(mylong); location locationb = new location("b"); locationb.setlatitude(pointlat); locationb.setlongitude(pointlong); distance = locationa.distanceto(locationb); return distance / 1000; } @override public void onlocationchanged(location location) { final location loc = location; mylat = loc.getlatitude(); mylong = location.getlongitude(); log.d("mylogs", "-------- onlochangedend " + mylat + " " + mylong); calculatedistance();---i want calculate distance after location } @override public void onstop() { db.close(); if (c != null) { c.close(); } super.onstop(); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } public void calculatedistance() { c = db.getplacebygroup(3); if (c != null) { if (c.movetofirst()) { { string s = c.getstring(8); double lat, lon; lat = double.valueof(s.substring(0, s.indexof(","))); lon = double.valueof(s.substring(s.indexof(",") + 1, s.length())); log.d("mylogs", "id= " + c.getint(0) + "coordinates" + c.getstring(8));// !! contentvalues cv = new contentvalues(); cv.put(database.place_distance, getdistance(mylat, mylong, lat, lon)); int updcount = db.mdb.update(database.db_table_places, cv, "_id = " + db.getplaceid(3, c.getposition()), null); log.d("mylogs", "updated rows count = " + updcount); } while (c.movetonext()); // log.d("mylogs","n1ame =" +c.getstring(1)); } } else log.d("mylogs", "cursor null"); c = db.getplacesmore5000(); scadapter = new simplecursoradapter(getactivity(), r.layout.item_near_list, c, from, to); lvmore5.setcachecolorhint(color.transparent); lvmore5.setadapter(scadapter); lvmore5.setontouchlistener(new listview.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { int action = event.getaction(); switch (action) { case motionevent.action_down: // disallow scrollview intercept touch events. v.getparent().requestdisallowintercepttouchevent(true); break; case motionevent.action_up: // allow scrollview intercept touch events. v.getparent().requestdisallowintercepttouchevent(false); break; } // handle listview touch events. v.ontouchevent(event); return true; } }); lvmore5.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { fragment fragment = new fragmentplaceinfo(); db.open(); bundle data = new bundle(); c = db.getplacesmore5000(); if (c != null) { c.movetoposition(position); data.putint("placeid", c.getint(0)); } else log.d("mylogs", "cursor null"); fragment.setarguments(data); fragmenttransaction transaction = getfragmentmanager() .begintransaction(); transaction.replace(r.id.content_frame, fragment); transaction.addtobackstack(null); transaction.commit(); // /less 1 } }); } @override public void onproviderenabled(string provider) { // todo auto-generated method stub } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } }
real device - wgen press r.id.btn_near - nice, when pressed again( want run fragment again) - error
08-23 17:52:39.928: e/androidruntime(2953): fatal exception: main 08-23 17:52:39.928: e/androidruntime(2953): java.lang.nullpointerexception 08-23 17:52:39.928: e/androidruntime(2953): @ android.support.v4.widget.resourcecursoradapter.<init>(resourcecursoradapter.java:56) 08-23 17:52:39.928: e/androidruntime(2953): @ android.support.v4.widget.simplecursoradapter.<init>(simplecursoradapter.java:63) 08-23 17:52:39.928: e/androidruntime(2953): @ com.example.cars51ru.fragmentnear.showmfd(fragmentnear.java:352) 08-23 17:52:39.928: e/androidruntime(2953): @ com.example.cars51ru.fragmentnear.onlocationchanged(fragmentnear.java:124) 08-23 17:52:39.928: e/androidruntime(2953): @ android.location.locationmanager$listenertransport._handlemessage(locationmanager.java:227) 08-23 17:52:39.928: e/androidruntime(2953): @ android.location.locationmanager$listenertransport.access$000(locationmanager.java:160) 08-23 17:52:39.928: e/androidruntime(2953): @ android.location.locationmanager$listenertransport$1.handlemessage(locationmanager.java:176) 08-23 17:52:39.928: e/androidruntime(2953): @ android.os.handler.dispatchmessage(handler.java:99) 08-23 17:52:39.928: e/androidruntime(2953): @ android.os.looper.loop(looper.java:123) 08-23 17:52:39.928: e/androidruntime(2953): @ android.app.activitythread.main(activitythread.java:3839) 08-23 17:52:39.928: e/androidruntime(2953): @ java.lang.reflect.method.invokenative(native method) 08-23 17:52:39.928: e/androidruntime(2953): @ java.lang.reflect.method.invoke(method.java:507) 08-23 17:52:39.928: e/androidruntime(2953): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:841) 08-23 17:52:39.928: e/androidruntime(2953): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:599) 08-23 17:52:39.928: e/androidruntime(2953): @ dalvik.system.nativestart.main(native method)
in line : scadapter = new simplecursoradapter(getactivity(), r.layout.item_near_list, c, from, to);
when start on emulator - empty listview.
try put , before init of adapter, this:
db = new database(getactivity()); db.open(); = new string[] { database.place_name, database.place_distance, database.place_adress }; = new int[] { r.id.tv_place_name_n, r.id.tv_distance, r.id.tv_place_adress_n }; c = db.getplacesmore5000(); scadapter = new simplecursoradapter(getactivity(), r.layout.item_near_list, c, from, to);
Comments
Post a Comment