sockets - Android Development - Why is this application crashing? -
i've made small android application sends udp packet moment open it.
the problem is, crashes when open it, instead of sending packet.
i mean, i've tried running on eclipse's android virtual device, , crashes there.
here's code:
package com.example.messagesender; import android.os.bundle; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.app.activity; import java.io.*; import java.net.*; public class message_sender extends activity { datagramsocket clientsocket; inetaddress ipaddressx; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_message__sender); try { clientsocket = new datagramsocket(); } catch (socketexception e) { // todo auto-generated catch block e.printstacktrace(); } try { ipaddressx = inetaddress.getbyname("127.0.0.1"); } catch (unknownhostexception e) { e.printstacktrace(); } byte[] senddata = new byte[1024]; string sentence = "hello world!"; senddata = sentence.getbytes(); datagrampacket sendpacket = new datagrampacket(senddata, senddata.length, ipaddressx, 12345); try { clientsocket.send(sendpacket); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.message__sender, menu); return true; } }
logcat output:
01-31 19:00:58.420: w/system.err(810): java.net.socketexception: socket failed: eacces (permission denied) 01-31 19:00:58.420: w/system.err(810): @ libcore.io.iobridge.socket(iobridge.java:576) 01-31 19:00:58.420: w/system.err(810): @ java.net.plaindatagramsocketimpl.create(plaindatagramsocketimpl.java:91) 01-31 19:00:58.420: w/system.err(810): @ java.net.datagramsocket.createsocket(datagramsocket.java:133) 01-31 19:00:58.540: w/system.err(810): @ java.net.datagramsocket.<init>(datagramsocket.java:80) 01-31 19:00:58.540: w/system.err(810): @ java.net.datagramsocket.<init>(datagramsocket.java:65) 01-31 19:00:58.540: w/system.err(810): @ com.example.messagesender.message_sender.oncreate(message_sender.java:23) 01-31 19:00:58.540: w/system.err(810): @ android.app.activity.performcreate(activity.java:5231) 01-31 19:00:58.540: w/system.err(810): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 01-31 19:00:58.540: w/system.err(810): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 01-31 19:00:58.540: w/system.err(810): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 01-31 19:00:58.540: w/system.err(810): @ android.app.activitythread.access$800(activitythread.java:135) 01-31 19:00:58.540: w/system.err(810): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 01-31 19:00:58.540: w/system.err(810): @ android.os.handler.dispatchmessage(handler.java:102) 01-31 19:00:58.540: w/system.err(810): @ android.os.looper.loop(looper.java:136) 01-31 19:00:58.590: w/system.err(810): @ android.app.activitythread.main(activitythread.java:5017) 01-31 19:00:58.590: w/system.err(810): @ java.lang.reflect.method.invokenative(native method) 01-31 19:00:58.590: w/system.err(810): @ java.lang.reflect.method.invoke(method.java:515) 01-31 19:00:58.590: w/system.err(810): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 01-31 19:00:58.590: w/system.err(810): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 01-31 19:00:58.590: w/system.err(810): @ dalvik.system.nativestart.main(native method) 01-31 19:00:58.590: w/system.err(810): caused by: libcore.io.errnoexception: socket failed: eacces (permission denied) 01-31 19:00:58.610: w/system.err(810): @ libcore.io.posix.socket(native method) 01-31 19:00:58.610: w/system.err(810): @ libcore.io.blockguardos.socket(blockguardos.java:181) 01-31 19:00:58.610: w/system.err(810): @ libcore.io.iobridge.socket(iobridge.java:561) 01-31 19:00:58.610: w/system.err(810): ... 19 more 01-31 19:00:58.630: d/androidruntime(810): shutting down vm 01-31 19:00:58.630: w/dalvikvm(810): threadid=1: thread exiting uncaught exception (group=0xb1a86ba8) 01-31 19:00:58.640: e/androidruntime(810): fatal exception: main 01-31 19:00:58.640: e/androidruntime(810): process: com.example.messagesender, pid: 810 01-31 19:00:58.640: e/androidruntime(810): java.lang.runtimeexception: unable start activity componentinfo{com.example.messagesender/com.example.messagesender.message_sender}: java.lang.nullpointerexception 01-31 19:00:58.640: e/androidruntime(810): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 01-31 19:00:58.640: e/androidruntime(810): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 01-31 19:00:58.640: e/androidruntime(810): @ android.app.activitythread.access$800(activitythread.java:135) 01-31 19:00:58.640: e/androidruntime(810): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 01-31 19:00:58.640: e/androidruntime(810): @ android.os.handler.dispatchmessage(handler.java:102) 01-31 19:00:58.640: e/androidruntime(810): @ android.os.looper.loop(looper.java:136) 01-31 19:00:58.640: e/androidruntime(810): @ android.app.activitythread.main(activitythread.java:5017) 01-31 19:00:58.640: e/androidruntime(810): @ java.lang.reflect.method.invokenative(native method) 01-31 19:00:58.640: e/androidruntime(810): @ java.lang.reflect.method.invoke(method.java:515) 01-31 19:00:58.640: e/androidruntime(810): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 01-31 19:00:58.640: e/androidruntime(810): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 01-31 19:00:58.640: e/androidruntime(810): @ dalvik.system.nativestart.main(native method) 01-31 19:00:58.640: e/androidruntime(810): caused by: java.lang.nullpointerexception 01-31 19:00:58.640: e/androidruntime(810): @ com.example.messagesender.message_sender.oncreate(message_sender.java:38) 01-31 19:00:58.640: e/androidruntime(810): @ android.app.activity.performcreate(activity.java:5231) 01-31 19:00:58.640: e/androidruntime(810): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 01-31 19:00:58.640: e/androidruntime(810): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 01-31 19:00:58.640: e/androidruntime(810): ... 11 more
so why crashing? code looks fine me, it's weird.
it looks trying make network calls on ui thread, big no no.
take @ how implement network operations using background threads, asynctask
there literaly 1000's of questions , examples this.
here tutorial started
also add necessary permission manifest
<uses-permission android:name="android.permission.internet" />
Comments
Post a Comment