NSD is a service used to discover other applications running the same service on the local network. I'm trying to get the NSD example that you find on the official Android site to work .
The problem is that when selecting Search the application crashes only on Android 6 (I have a tablet with android 4.4.4 and it does not give the error). When you reopen the app, the same thing happens. The error is the following:
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4741)
at android.view.View.performClick(View.java:5698)
at android.widget.TextView.performClick(TextView.java:10846)
at android.view.View$PerformClick.run(View.java:22565)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4736)
at android.view.View.performClick(View.java:5698)
at android.widget.TextView.performClick(TextView.java:10846)
at android.view.View$PerformClick.run(View.java:22565)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.IllegalArgumentException: listener already in use
at android.net.nsd.NsdManager.discoverServices(NsdManager.java:559)
at com.miaplicacion.configurador.NsdHelper.discoverServices(NsdHelper.java:140)
at com.miaplicacion.configurador.NsdChatActivity.clickDiscover(NsdChatActivity.java:58)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4736)
at android.view.View.performClick(View.java:5698)
at android.widget.TextView.performClick(TextView.java:10846)
at android.view.View$PerformClick.run(View.java:22565)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
I think the main error is because "listener already in use". But I don't know how to fix it.
These are the additional codes:
activity_main.xml (the button code)
<Button
android:id="@+id/discover_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="clickDiscover"
android:text="Buscar" />
NsdChatActivity (the code of the Activity that receives the click event)
public void clickDiscover(View v) {
mNsdHelper.discoverServices();
}
NsdHelper
public void discoverServices() {
mNsdManager.discoverServices(
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
In the documentation it says:
The problem seems to be a century old of your listener. Probably the
mDiscoveryListener
one you pass is already in use. Try creating a Listener by implementingDiscoveryListener
what you urge and save todiscoverServices
.and create it at the moment when you start discovering services.
You're probably going to have to design a bit more precisely the life cycles of your activities and when service discovery starts.
The real problem in this example on NSD is:
What you have to ensure is not to use the same defined listener, for this you can create a class to not use the same listener instance:
The class in question would be:
I add the complete NsdHelper class: