java - Bluetooth Messenger application crashing at start up itself -
i have tried learn write application bluetooth messenger. things had been fine unless tried install , run it. gets installed crashes down @ start itself. can please tell me wrong ? code main activity. there 3 more activities apart this.
import java.io.ioexception; import java.util.arraylist; import android.app.listactivity; import android.bluetooth.bluetoothadapter; import android.bluetooth.bluetoothdevice; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.os.asynctask; import android.os.bundle; import android.os.handler; import android.os.message; import android.util.log; import android.view.menu; import android.view.view; import android.widget.arrayadapter; import android.widget.edittext; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class mainactivity extends listactivity { public final static string uuid = "3606f360-e4df-11e0-9572-0800200c9a66"; bluetoothadapter bluetoothadapter; broadcastreceiver discoverdevicesreceiver; broadcastreceiver discoveryfinishedreceiver; //---store discovered devices--- arraylist<bluetoothdevice> discovereddevices; arraylist<string> discovereddevicesnames; //---store paired devices--- arraylist<bluetoothdevice> paireddevices; static textview txtdata; edittext txtmessage; //---thread running server socket--- serverthread serverthread; //---thread connecting client socket--- connecttoserverthread connecttoserverthread; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //---init arraylist objects , bluetooth adapter--- discovereddevices = new arraylist<bluetoothdevice>(); discovereddevicesnames = new arraylist<string>(); paireddevices = new arraylist<bluetoothdevice>(); bluetoothadapter = bluetoothadapter.getdefaultadapter(); //---for displaying messages received--- txtdata = (textview) findviewbyid(r.id.txtdata); txtmessage = (edittext) findviewbyid(r.id.txtmessage); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_main, menu); return true; } //---make discoverable--- public void makediscoverable(view view) { intent = new intent( bluetoothadapter.action_request_discoverable); i.putextra( bluetoothadapter.extra_discoverable_duration, 300); startactivity(i); } /* //---find paired devices--- private void querypaireddevices(){ set<bluetoothdevice> allpaireddevices = bluetoothadapter.getbondeddevices(); //---if there paired devices--- if (allpaireddevices.size() > 0) { //---loop through paired devices--- (bluetoothdevice device : allpaireddevices) { //---add name , address array adapter // show in listview--- log.d("usingbluetooth", device.getname() + "\n" + device.getaddress()); paireddevices.add(device); } } } */ //---used discover other bluetooth devices--- private void discoveringdevices() { if (discoverdevicesreceiver == null) { discoverdevicesreceiver = new broadcastreceiver() { //---fired when new device discovered--- @override public void onreceive(context context, intent intent) { string action = intent.getaction(); //---a device discovered--- if (bluetoothdevice.action_found.equals(action)) { //---get bluetoothdevice object // intent--- bluetoothdevice device = intent.getparcelableextra( bluetoothdevice.extra_device); //---add name , address array // adapter show in listview--- //---only add if device not // in list--- if (!discovereddevices.contains(device)) { //---add device--- discovereddevices.add(device); //---add name of device; used // listview--- discovereddevicesnames.add(device.getname()); //---display items in listview--- setlistadapter(new arrayadapter<string>(getbasecontext(), android.r.layout.simple_list_item_1, discovereddevicesnames)); } } } }; } if (discoveryfinishedreceiver==null) { discoveryfinishedreceiver = new broadcastreceiver() { //---fired when discovery done--- @override public void onreceive(context context, intent intent) { //---enable listview when discovery over; 12 seconds--- getlistview().setenabled(true); toast.maketext(getbasecontext(), "discovery completed. select device start chatting.", toast.length_long).show(); unregisterreceiver(discoveryfinishedreceiver); } }; } //---register broadcast receivers--- intentfilter filter1 = new intentfilter(bluetoothdevice.action_found); intentfilter filter2 = new intentfilter(bluetoothadapter.action_discovery_finished); registerreceiver(discoverdevicesreceiver, filter1); registerreceiver(discoveryfinishedreceiver, filter2); //---disable listview when discover in progress--- getlistview().setenabled(false); toast.maketext(getbasecontext(), "discovery in progress...please wait...", toast.length_long).show(); bluetoothadapter.startdiscovery(); } //---discover other bluetooth devices--- public void discoverdevices(view view) { //---query paired devices--- //querypaireddevices(); //---discover other devices--- discoveringdevices(); } @override public void onpause() { super.onpause(); //---cancel discovery of other bluetooth devices bluetoothadapter.canceldiscovery(); //---unregister broadcast receiver // discovering devices--- if (discoverdevicesreceiver != null) { try { unregisterreceiver(discoverdevicesreceiver); } catch(exception e) { } } //---if connected someone...--- if (connecttoserverthread!=null) { try { //---close connection--- connecttoserverthread.bluetoothsocket.close(); } catch (ioexception e) { log.d("mainactivity", e.getlocalizedmessage()); } } //---stop thread running--- if (serverthread!=null) serverthread.cancel(); } //---used updating ui on main activity--- static handler uiupdater = new handler() { @override public void handlemessage(message msg) { int numofbytesreceived = msg.arg1; byte[] buffer = (byte[]) msg.obj; //---convert entire byte array string--- string strreceived = new string(buffer); //---extract actual string received--- strreceived = strreceived.substring( 0, numofbytesreceived); //---display text received on textview--- txtdata.settext(txtdata.gettext().tostring() + strreceived); } }; @override public void onresume() { super.onresume(); //---start socket server--- serverthread = new serverthread(bluetoothadapter); serverthread.start(); } //---when client tapped in listview--- public void onlistitemclick(listview parent, view v, int position, long id) { //---if talking someone...--- if (connecttoserverthread!=null) { try { //---close connection first--- connecttoserverthread.bluetoothsocket.close(); } catch (ioexception e) { log.d("mainactivity", e.getlocalizedmessage()); } } //---connect selected bluetooth device--- bluetoothdevice deviceselected = discovereddevices.get(position); connecttoserverthread = new connecttoserverthread(deviceselected, bluetoothadapter); connecttoserverthread.start(); //---tell user connected who--- toast.maketext(this, "you have connected " + discovereddevices.get(position).getname(), toast.length_short).show(); } private class writetask extends asynctask<string, void, void> { protected void doinbackground(string... args) { try { connecttoserverthread.commsthread.write(args[0]); } catch (exception e) { log.d("mainactivity", e.getlocalizedmessage()); } return null; } } //---send message connected socket client--- public void sendmessage(view view) { if (connecttoserverthread!=null) { ///========= //connecttoserverthread.commsthread.write( // txtmessage.gettext().tostring()); new writetask().execute(txtmessage.gettext().tostring()); ///========= } else { toast.maketext(this, "select client first", toast.length_short).show(); } } } serverthread class :
import java.io.ioexception; import java.util.uuid; import android.bluetooth.bluetoothadapter; import android.bluetooth.bluetoothserversocket; import android.bluetooth.bluetoothsocket; import android.util.log; public class serverthread extends thread { //---the server socket--- private final bluetoothserversocket bluetoothserversocket; public serverthread(bluetoothadapter bluetoothadapter) { bluetoothserversocket tmp = null; try { //---uuid must same both client , // server--- tmp = bluetoothadapter.listenusingrfcommwithservicerecord( "bluetoothapp", uuid.fromstring(mainactivity.uuid)); } catch (ioexception e) { log.d("serverthread", e.getlocalizedmessage()); } bluetoothserversocket = tmp; } public void run() { bluetoothsocket socket = null; //---keep listening until exception occurs // or socket returned--- while (true) { try { socket = bluetoothserversocket.accept(); } catch (ioexception e) { log.d("serverthread", e.getlocalizedmessage()); break; } //---if connection accepted--- if (socket != null) { //---create separate thread listen // incoming data--- commsthread commsthread = new commsthread(socket); commsthread.run(); } } } public void cancel() { try { bluetoothserversocket.close(); } catch (ioexception e) { log.d("serverthread", e.getlocalizedmessage()); } } } logcat
04-13 18:25:06.684: d/androidruntime(397): shutting down vm 04-13 18:25:06.684: w/dalvikvm(397): threadid=1: thread exiting uncaught exception (group=0x40015560) 04-13 18:25:06.704: e/androidruntime(397): fatal exception: main 04-13 18:25:06.704: e/androidruntime(397): java.lang.runtimeexception: unable resume activity {net.learn2develop.usingbluetooth/net.learn2develop.usingbluetooth.mainactivity}: java.lang.nullpointerexception 04-13 18:25:06.704: e/androidruntime(397): @ android.app.activitythread.performresumeactivity(activitythread.java:2120) 04-13 18:25:06.704: e/androidruntime(397): @ android.app.activitythread.handleresumeactivity(activitythread.java:2135) 04-13 18:25:06.704: e/androidruntime(397): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1668) 04-13 18:25:06.704: e/androidruntime(397): @ android.app.activitythread.access$1500(activitythread.java:117) 04-13 18:25:06.704: e/androidruntime(397): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 04-13 18:25:06.704: e/androidruntime(397): @ android.os.handler.dispatchmessage(handler.java:99) 04-13 18:25:06.704: e/androidruntime(397): @ android.os.looper.loop(looper.java:123) 04-13 18:25:06.704: e/androidruntime(397): @ android.app.activitythread.main(activitythread.java:3683) 04-13 18:25:06.704: e/androidruntime(397): @ java.lang.reflect.method.invokenative(native method) 04-13 18:25:06.704: e/androidruntime(397): @ java.lang.reflect.method.invoke(method.java:507) 04-13 18:25:06.704: e/androidruntime(397): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 04-13 18:25:06.704: e/androidruntime(397): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 04-13 18:25:06.704: e/androidruntime(397): @ dalvik.system.nativestart.main(native method) 04-13 18:25:06.704: e/androidruntime(397): caused by: java.lang.nullpointerexception 04-13 18:25:06.704: e/androidruntime(397): @ net.learn2develop.usingbluetooth.serverthread.<init>(serverthread.java:21) 04-13 18:25:06.704: e/androidruntime(397): @ net.learn2develop.usingbluetooth.mainactivity.onresume(mainactivity.java:235) 04-13 18:25:06.704: e/androidruntime(397): @ android.app.instrumentation.callactivityonresume(instrumentation.java:1150) 04-13 18:25:06.704: e/androidruntime(397): @ android.app.activity.performresume(activity.java:3832) 04-13 18:25:06.704: e/androidruntime(397): @ android.app.activitythread.performresumeactivity(activitythread.java:2110) 04-13 18:25:06.704: e/androidruntime(397): ... 12 more 04-13 18:25:08.764: i/process(397): sending signal. pid: 397 sig: 9 04-13 18:25:10.354: d/androidruntime(415): shutting down vm 04-13 18:25:10.354: w/dalvikvm(415): threadid=1: thread exiting uncaught exception (group=0x40015560) 04-13 18:25:10.364: e/androidruntime(415): fatal exception: main 04-13 18:25:10.364: e/androidruntime(415): java.lang.runtimeexception: unable resume activity {net.learn2develop.usingbluetooth/net.learn2develop.usingbluetooth.mainactivity}: java.lang.nullpointerexception 04-13 18:25:10.364: e/androidruntime(415): @ android.app.activitythread.performresumeactivity(activitythread.java:2120) 04-13 18:25:10.364: e/androidruntime(415): @ android.app.activitythread.handleresumeactivity(activitythread.java:2135) 04-13 18:25:10.364: e/androidruntime(415): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1668) 04-13 18:25:10.364: e/androidruntime(415): @ android.app.activitythread.access$1500(activitythread.java:117) 04-13 18:25:10.364: e/androidruntime(415): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 04-13 18:25:10.364: e/androidruntime(415): @ android.os.handler.dispatchmessage(handler.java:99) 04-13 18:25:10.364: e/androidruntime(415): @ android.os.looper.loop(looper.java:123) 04-13 18:25:10.364: e/androidruntime(415): @ android.app.activitythread.main(activitythread.java:3683) 04-13 18:25:10.364: e/androidruntime(415): @ java.lang.reflect.method.invokenative(native method) 04-13 18:25:10.364: e/androidruntime(415): @ java.lang.reflect.method.invoke(method.java:507) 04-13 18:25:10.364: e/androidruntime(415): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 04-13 18:25:10.364: e/androidruntime(415): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 04-13 18:25:10.364: e/androidruntime(415): @ dalvik.system.nativestart.main(native method) 04-13 18:25:10.364: e/androidruntime(415): caused by: java.lang.nullpointerexception 04-13 18:25:10.364: e/androidruntime(415): @ net.learn2develop.usingbluetooth.serverthread.<init>(serverthread.java:21) 04-13 18:25:10.364: e/androidruntime(415): @ net.learn2develop.usingbluetooth.mainactivity.onresume(mainactivity.java:235) 04-13 18:25:10.364: e/androidruntime(415): @ android.app.instrumentation.callactivityonresume(instrumentation.java:1150) 04-13 18:25:10.364: e/androidruntime(415): @ android.app.activity.performresume(activity.java:3832) 04-13 18:25:10.364: e/androidruntime(415): @ android.app.activitythread.performresumeactivity(activitythread.java:2110) 04-13 18:25:10.364: e/androidruntime(415): ... 12 more 04-13 18:25:13.304: i/process(415): sending signal. pid: 415 sig: 9
possible changes can try might you.
in serverthread.java
public void run() { .... while(!thread.currentthread().isinterrupted()){ //do } } if works fine, otherwise try: in mainactivity.java
@override public void oncreate() { .... //start thread here //---start socket server--- serverthread = new serverthread(bluetoothadapter); serverthread.start(); } @override public void onresume() { super.onresume(); //handle thread here - following maynot helpful, logiaclly speaking, //you must handle threads here if(serverthread.isalive()) serverthread.resume(); else serverthread.interrupt(); }
Comments
Post a Comment