java - Get data between Activity and fragment with googleMaps -
introduction
well, have main activity has:
- 1 button starts googlemaps api
- 2 edittext show latitude , longitude coordinates
i mannage use of smartphone or tablet, i'm using fragment map. when use tablet, shows map @ right side of main acitivity (i use framelayout on xml file), , when use smartphone, throws intent show new activity map.
but, mapsfragment isn't activity, can't call intent, i've created intermediate activity.
the structure this: mainactivity(geocodingactivity) -> googlemapsactivity-> googlemapsfragment
functionality
the use of app simple. when click on map button, starts googlemaps. there, click on position of map , must return position's coordinates main activity , show them in edittexts.
code
this code use start googlemaps:
/**on button click in main activity*/ intent intent = new intent(); intent.setclass(geocodingactivity.this, googlemapsactivity.class); startactivityforresult(intent, request_code_map_location); /**in googlemapsactivity's oncreate method*/ googlemapsfragment mapsfragment = new googlemapsfragment(); fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); ft.add(android.r.id.content, mapsfragment).commit();
to send data googlemapsfragment mainactivity:
/**on map long click starts returnmapdata method*/ mmap.setonmaplongclicklistener(new onmaplongclicklistener() { @override public void onmaplongclick(latlng arg0) { mlatlng = arg0; returnmapdata(mlatlng); } }); /**method returns data form googlemapsfragment mainactivity*/ public void returnmapdata(latlng arg) { intent intent = new intent(); bundle bundle = new bundle(); bundle.putparcelable("maps_location", arg); intent.putextra("bundle", bundle); getactivity().setresult(getactivity().result_ok, intent); }
and ant last receive data in mainacitivty onactivityresult method:
protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == request_code_map_location) { if(resultcode == result_ok){ bundle bundle = data.getbundleextra("bundle"); latlng coordinates = bundle.getparcelable("maps_location"); mlocation.setlatitude(coordinates.latitude); mlocation.setlongitude(coordinates.longitude); } } }
issue
it seems ok, on logcat. happens in onactivityresult method.
02-06 09:26:43.210: e/androidruntime(20541): fatal exception: main 02-06 09:26:43.210: e/androidruntime(20541): java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=0, result=-1, data=intent { cmp=com.uax_final.reversegeocoding/.googlemapsactivity (has extras) }} activity {com.uax_final.reversegeocoding/com.uax_final.reversegeocoding.geocodingactivity}: java.lang.nullpointerexception 02-06 09:26:43.210: e/androidruntime(20541): @ android.app.activitythread.deliverresults(activitythread.java:3500) 02-06 09:26:43.210: e/androidruntime(20541): @ android.app.activitythread.handlesendresult(activitythread.java:3543) 02-06 09:26:43.210: e/androidruntime(20541): @ android.app.activitythread.access$1200(activitythread.java:159) 02-06 09:26:43.210: e/androidruntime(20541): @ android.app.activitythread$h.handlemessage(activitythread.java:1364) 02-06 09:26:43.210: e/androidruntime(20541): @ android.os.handler.dispatchmessage(handler.java:99) 02-06 09:26:43.210: e/androidruntime(20541): @ android.os.looper.loop(looper.java:137) 02-06 09:26:43.210: e/androidruntime(20541): @ android.app.activitythread.main(activitythread.java:5419) 02-06 09:26:43.210: e/androidruntime(20541): @ java.lang.reflect.method.invokenative(native method) 02-06 09:26:43.210: e/androidruntime(20541): @ java.lang.reflect.method.invoke(method.java:525) 02-06 09:26:43.210: e/androidruntime(20541): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1187) 02-06 09:26:43.210: e/androidruntime(20541): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1003) 02-06 09:26:43.210: e/androidruntime(20541): @ dalvik.system.nativestart.main(native method) 02-06 09:26:43.210: e/androidruntime(20541): caused by: java.lang.nullpointerexception 02-06 09:26:43.210: e/androidruntime(20541): @ com.uax_final.reversegeocoding.geocodingactivity.onactivityresult(geocodingactivity.java:232) 02-06 09:26:43.210: e/androidruntime(20541): @ android.app.activity.dispatchactivityresult(activity.java:5563) 02-06 09:26:43.210: e/androidruntime(20541): @ android.app.activitythread.deliverresults(activitythread.java:3496)
update 1 -- posible reason this
i've seen problem when returning data googlemapsfragment mainactivity objects null, , think because of structure use.
mainactivity(geocodingactivity) -> googlemapsactivity-> googlemapsfragment
when pass data googlemapsfragment mainactivity, activity openend still googlemapsactivity, , reason why occurs.
now, thing is... how close googlemapsactivity when have requested data in googlemapsfragment , want mainactivity???
i answer myself:
the problem in onactivityresult method.
the mlocation object null can't use setlatitude , setlongitude methods.
the right way this:
location position = new location("test"); position.setlatitude(coordinates.latitude); position.setlongitude(coordinates.longitude); mlocation = position;
Comments
Post a Comment