1

Link to the 3 java files: https://www.dropbox.com/s/f57xwvm3gu9eubm/macnetworknotify.rar?dl=0

I am trying to execute this application example found at http://developer.android.com/training/connect-devices-wirelessly/nsd.html#discover about NSD. When I execute it, it says that it could not execute method of the activity when I click on the 'discover' button. All other buttons have no problem.

09-06 17:16:10.430  13489-13489/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.motivecodex.macnetworknotify, PID: 13489
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4253)
            at android.view.View.performClick(View.java:5197)
            at android.view.View$PerformClick.run(View.java:20926)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4248)
            at android.view.View.performClick(View.java:5197)
            at android.view.View$PerformClick.run(View.java:20926)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
     Caused by: java.lang.IllegalArgumentException: listener already in use
            at android.net.nsd.NsdManager.discoverServices(NsdManager.java:559)
            at com.motivecodex.macnetworknotify.NsdHelper.discoverServices(NsdHelper.java:141)
            at com.motivecodex.macnetworknotify.MainActivity.clickDiscover(MainActivity.java:58)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4248)
            at android.view.View.performClick(View.java:5197)
            at android.view.View$PerformClick.run(View.java:20926)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

Some code I think might help sove the problem, I really don't know or can't find the problem.

activity_main.xml

<Button
 android:id="@+id/discover_btn"
 android:layout_width="96dp"
 android:layout_height="64dp"
 android:onClick="clickDiscover"
 android:text="@string/discover" />

MainActivity.java

public void clickDiscover(View v) {
        mNsdHelper.discoverServices();
    }

@Override
    protected void onResume() {
        super.onResume();
        if (mNsdHelper != null) {
            mNsdHelper.discoverServices();
        }
    }

NsdHelper.java

public void discoverServices() {
        mNsdManager.discoverServices(
                SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
    }

public void stopDiscovery() {
        mNsdManager.stopServiceDiscovery(mDiscoveryListener);
    }

And, when I open NsdManager.java, where discoveryServices and NsdManager are located, I see a lot of errors, starting from the import saying cannot resolve SdkConstant and AsyncChannel and Protocol and then a lot of erros within the code. Is this normal? Is this causing the crash?

Onik
  • 19,396
  • 14
  • 68
  • 91
MOTIVECODEX
  • 2,624
  • 14
  • 43
  • 78

2 Answers2

1

The problem seems to be cause by the discoverServices(...) method call, you're reusing the mDiscoveryListener listener.

public void discoverServices (String serviceType, int protocolType, NsdManager.DiscoveryListener listener)

listener .... Cannot be null. Cannot be in use for an active service discovery.

Titus
  • 22,031
  • 1
  • 23
  • 33
  • could it be that it does not discover any other device (that's what it want to do) and crashes therefore? Just asking, because I had a similar crash experience when I could not find anyone within facebook. – MOTIVECODEX Sep 06 '15 at 15:57
  • @F4LLCON I don't think so, the problem may be cause because you're calling the `discoverServices()` more then once or you're using the `mDiscoveryListener` somewhere else in your app. You can't reuse the listener until you call the `stopServiceDiscovery(NsdManager.DiscoveryListener)` method. – Titus Sep 06 '15 at 16:01
  • I don't think I am calling the discoveryServices() somewhere else and also don't use the mDiscoveryListener anywhere else.. I've added the Java files to my dropbox. Link is at the beginning of my question – MOTIVECODEX Sep 06 '15 at 16:09
  • @F4LLCON you're calling the `discoverServices()` multiple times, it is called in the `onResume()` method and in the `clickDiscover(...)` method. – Titus Sep 06 '15 at 16:21
  • I am calling the discoveryServices() that is inside NsdHelper from within MainActivity.java. So basically it's being called one time? The code is literally from the Google example apps found here http://developer.android.com/intl/es/training/connect-devices-wirelessly/nsd.html – MOTIVECODEX Sep 06 '15 at 16:34
  • @F4LLCON `discoveryServices()` is called multiple times (every time the activity is resumed or the button clicked) and that is what is causing the problem. Inside the `discoverServices()` you are calling `discoverServices(...)` and you're reusing the `mDiscoveryListener` listener. To fix this, you can use a mechanism to keep track if the service discovery was stopped before you start it again. – Titus Sep 06 '15 at 16:41
0

Have you managed to make it worked, and how about adding a check flag before discovering?

boolean isListenerUsed = false;

And:

if (isListenerUsed == false) {
        isListenerUsed = true;
        mNsdManager.discoverServices(
                SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
Tim
  • 606
  • 1
  • 8
  • 19