javaandroidandroid-nsd

Discover button IllegalStateException: Could not execute method of the activity


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?


Solution

  • 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.