java - Making android application using pubnub to send GPS information but app crashes and shows no errors -
i making application sends realtime messages through pubnub server users subscribed same channel. have working can send strings device device, want send gps information. user input string "gps" , trigger method send location info instead of string. having trouble part because no errors shown, app crashes when try open it. tried debugging, can figure out whats going wrong. debugger takes me out of activity , these other java class files embedded in java after step on oncreate method. or feedback appreciated, thanks!
some errors found in logcat:
adb:
ddms: null java.nio.bufferoverflowexception @ java.nio.heapbytebuffer.put(heapbytebuffer.java:182) @ com.android.ddmlib.jdwppacket.movepacket(jdwppacket.java:235) @ com.android.ddmlib.debugger.sendandconsume(debugger.java:347) @ com.android.ddmlib.client.forwardpackettodebugger(client.java:698) @ com.android.ddmlib.monitorthread.processclientactivity(monitorthread.java:344) @ com.android.ddmlib.monitorthread.run(monitorthread.java:263)
device logcat:
03-20 15:44:56.780 15612-15612/com.example.kunalpatel.pubsub e/selinux﹕ function: selinux_android_load_priority [0], there no sepolicy file 03-20 15:44:56.780 15612-15612/com.example.kunalpatel.pubsub e/selinux﹕ function: selinux_android_load_priority [1], there no sepolicy version file 03-20 15:44:56.780 15612-15612/com.example.kunalpatel.pubsub e/selinux﹕ function: selinux_android_load_priority , loading version ve=sepf_samsung-sgh-i257_4.2.2_0025 03-20 15:44:56.780 15612-15612/com.example.kunalpatel.pubsub e/selinux﹕ selinux_android_seapp_context_reload: seapp_contexts file loaded /seapp_contexts java.lang.runtimeexception: unable resume activity {com.example.kunalpatel.pubsub/com.example.kunalpatel.pubsub.mainactivity}: java.lang.classcastexception: com.example.kunalpatel.pubsub.mainactivity cannot cast android.location.locationlistener @ android.app.activitythread.performresumeactivity(activitythread.java:2835) @ android.app.activitythread.handleresumeactivity(activitythread.java:2864) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2309) @ android.app.activitythread.access$700(activitythread.java:152) @ android.app.activitythread$h.handlemessage(activitythread.java:1284) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:176) @ android.app.activitythread.main(activitythread.java:5299) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) @ dalvik.system.nativestart.main(native method) caused by: java.lang.classcastexception: com.example.kunalpatel.pubsub.mainactivity cannot cast android.location.locationlistener @ com.example.kunalpatel.pubsub.mainactivity.onresume(mainactivity.java:221) @ android.app.instrumentation.callactivityonresume(instrumentation.java:1202) @ android.app.activity.performresume(activity.java:5404) @ android.app.activitythread.performresumeactivity(activitythread.java:2825) at android.app.activitythread.handleresumeactivity(activitythread.java:2864) at android.app.activitythread.handlelaunchactivity(activitythread.java:2309) at android.app.activitythread.access$700(activitythread.java:152) at android.app.activitythread$h.handlemessage(activitythread.java:1284) at android.os.handler.dispatchmessage(handler.java:99) at android.os.looper.loop(looper.java:176) at android.app.activitythread.main(activitythread.java:5299) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:511) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) at com.android.internal.os.zygoteinit.main(zygoteinit.java:869) at dalvik.system.nativestart.main(native method)
import android.content.context; import android.location.criteria; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.text.method.scrollingmovementmethod; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; import com.pubnub.api.callback; import com.pubnub.api.pubnub; import com.pubnub.api.pubnuberror; import com.pubnub.api.pubnubexception; public class mainactivity extends actionbaractivity { //button subscribe user specified pubnub channel private button channelsubscribebutton; private edittext subscribechanneledittext; private textview messagelogtextview; //button send message other devices subscribed on same channel private button sendmessagebutton; private edittext sendmessageedittext; private locationmanager locationmanager; private string provider; private double lat; private double lon; private double accuracy; private mainactivity activity; //-------------------------access pubnub api-------------------------// //pubnub publish , subscribe keys pubnub pubnub = new pubnub("pub-c-", "sub-c"); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //subscribing , sending messages channelsubscribebutton = (button) findviewbyid(r.id.subscribe_button); subscribechanneledittext = (edittext) findviewbyid(r.id.channel_name); messagelogtextview = (textview) findviewbyid(r.id.message_log_text_view); sendmessagebutton = (button) findviewbyid(r.id.send_message_button); sendmessageedittext = (edittext) findviewbyid(r.id.message_edit_text); activity = this; //get location manager locationmanager = (locationmanager) getsystemservice(context.location_service); //define criteria how select location provider --> use default criteria criteria = new criteria(); provider = locationmanager.getbestprovider(criteria, false); location location = locationmanager.getlastknownlocation(provider); //prints gps provider if there or isn't 1 if (location != null) { system.out.println("provider " + provider + " has been selected."); onlocationchanged(location); } else { //messagelogtextview.append("gps information not available"); } } @override public boolean oncreateoptionsmenu (menu menu){ // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected (menuitem item){ // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public void onchannelbuttonclick(view view) { //get user inputted text in string subscribechannel = string.valueof(subscribechanneledittext.gettext()); string yoursubscribechannel = "subscribed " + subscribechannel + " channel"; toast.maketext(this, yoursubscribechannel, toast.length_short).show(); try { pubnub.subscribe(subscribechannel, new callback() { @override public void connectcallback(string channel, object message) { log.d("pubnub", "subscribe : connect on channel:" + channel + " : " + message.getclass() + " : " + message.tostring()); } @override public void disconnectcallback(string channel, object message) { log.d("pubnub", "subscribe : disconnect on channel:" + channel + " : " + message.getclass() + " : " + message.tostring()); } public void reconnectcallback(string channel, object message) { log.d("pubnub", "subscribe : reconnect on channel:" + channel + " : " + message.getclass() + " : " + message.tostring()); } //updates textview message @override public void successcallback(string channel, object message) { log.d("pubnub", "subscribe : " + channel + " : " + message.getclass() + " : " + message.tostring()); updatetextview(message.tostring() + "\n"); } @override public void errorcallback(string channel, pubnuberror error) { log.d("pubnub", "subscribe : error on channel " + channel + " : " + error.tostring()); } } ); } catch (pubnubexception e) { log.d("pubnub", e.tostring()); } } public void updatetextview(final string message) { activity.runonuithread(new runnable() { @override public void run() { try{ messagelogtextview.append(message); } catch(exception ex) { log.d("pubnub", ex.getmessage()); } messagelogtextview.setmovementmethod(new scrollingmovementmethod()); } }); } public void onsendmessagebuttonclick(view view, location location) { string messagetosend = string.valueof(sendmessageedittext.gettext()); callback callback = new callback() { public void successcallback(string channel, object response) { } public void errorcallback(string channel, pubnuberror error) { log.d("pubnub", error.tostring()); } }; string subscribechannel = string.valueof(subscribechanneledittext.gettext()); pubnub.publish(subscribechannel, messagetosend, callback); //if user input string equal "gps" initialize onlocationchanged method //if not, print string log textview if(messagetosend.equals("gps")) { //print textview log onlocationchanged onlocationchanged(location); } else { updatetextview("gps cannot retrieved"); } } @override protected void onresume() { super.onresume(); locationmanager.requestlocationupdates(provider, 400, 1, (locationlistener) this); } @override protected void onpause() { super.onpause(); locationmanager.removeupdates((locationlistener) this); } public void onstatuschanged(string provider, int status, bundle extras) { //todo auto-generated method stub } //method retrieve location information public void onlocationchanged(location location) { double lat = (double) (location.getlatitude()); double lng = (double) (location.getlongitude()); float accuracy = (float) (location.getaccuracy()); double alt = (double) (location.getaltitude()); double speed = (double) (location.getspeed()); double heading = (double) (location.getbearing()); messagelogtextview.append(string.valueof(lat)); messagelogtextview.append(string.valueof(lng)); messagelogtextview.append(string.valueof(accuracy)); messagelogtextview.append(string.valueof(alt)); messagelogtextview.append(string.valueof(speed )); messagelogtextview.append(string.valueof(heading )); } }
your error (in middle of stack trace provided):
caused by: java.lang.classcastexception: com.example.kunalpatel.pubsub.mainactivity cannot cast android.location.locationlistener @ com.example.kunalpatel.pubsub.mainactivity.onresume(mainactivity.java:221)
examining mainactivity.onresume
, turns out you're casting mainactivity
locationlistener
. you'll need implement locationlistener
if want able pass mainactivity
locationmanager.requestlocationupdates
.
to this, first check documentation locationlistener. there 4 abstract methods, you've implemented 2 of them far - onstatuschanged
, , onlocationchanged
. need implement onproviderenabled
, , onproviderdisabled
. then, change class declaration to:
public class mainactivity extends actionbaractivity implements locationlistener
Comments
Post a Comment