I have an applicaction that comunicates with other devices in two different ways: Sending sound signals and sending messages through Wifi. To Handle the Wifi comunication I created a Service that have a Thread listening for Broadcast messages. For the sounds signals I have as well I thread listening. So in total two threads, that I want to kill when I want byt pressing a button.
In the main Activity I call this method when I press the button:
public void onStop() {
super.onStop();
mHelloService.stop();
//This is the Sounds Thread
mListenThread = null;
if(D) Log.e(TAG, "-- ON STOP --");
}
And in the Service I have this function to stop the Wifi-Thread
public synchronized void stop() {
System.out.println("Close service");
if (mHelloThread != null) mHelloThread = null;
}
I try this, but the threads keep working. I read in another question that someone recommend to use:
mHelloThread.interrupt();
But I tried it and I got these errors:
08-30 20:18:08.592: D/AndroidRuntime(7312): Shutting down VM 08-30 20:18:08.592: W/dalvikvm(7312): threadid=1: thread exiting with uncaught exception (group=0x419bc930) 08-30 20:18:08.592: E/AndroidRuntime(7312): FATAL EXCEPTION: main 08-30 20:18:08.592: E/AndroidRuntime(7312): java.lang.NullPointerException 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.nacho.SoundLocalizer.HelloMessage.stop(HelloMessage.java:82) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.nacho.SoundLocalizer.SoundLocalizer.onStop(SoundLocalizer.java:349) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.nacho.SoundLocalizer.SoundLocalizer$3.onClick(SoundLocalizer.java:405) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.view.View.performClick(View.java:4202) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.view.View$PerformClick.run(View.java:17340) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.os.Handler.handleCallback(Handler.java:725) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.os.Handler.dispatchMessage(Handler.java:92) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.os.Looper.loop(Looper.java:137) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.app.ActivityThread.main(ActivityThread.java:5039) 08-30 20:18:08.592: E/AndroidRuntime(7312): at java.lang.reflect.Method.invokeNative(Native Method) 08-30 20:18:08.592: E/AndroidRuntime(7312): at java.lang.reflect.Method.invoke(Method.java:511) 08-30 20:18:08.592: E/AndroidRuntime(7312): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 08-30 20:18:08.592: E/AndroidRuntime(7312): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-30 20:18:08.592: E/AndroidRuntime(7312): at dalvik.system.NativeStart.main(Native Method)
What do you recommend me to do?? Thank you very much