发现按钮 IllegalStateException:无法执行 activity 的方法

Discover button IllegalStateException: Could not execute method of the activity

Link 到 3 java 个文件: https://www.dropbox.com/s/f57xwvm3gu9eubm/macnetworknotify.rar?dl=0

我正在尝试执行在 http://developer.android.com/training/connect-devices-wirelessly/nsd.html#discover 上找到的关于 NSD 的应用程序示例。当我执行它时,它说当我点击 'discover' 按钮时它无法执行 activity 的方法。所有其他按钮都没有问题。

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

一些我认为可能有助于解决问题的代码,我真的不知道或找不到问题。

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);
    }

而且,当我打开 NsdManager.java,发现服务和 NsdManager 所在的位置时,我看到了很多错误,从导入开始说无法解析 SdkConstant 和 AsyncChannel 和协议,然后在代码。这是正常的吗?这是导致崩溃的原因吗?

问题似乎是由 discoverServices(...) 方法调用引起的,您正在重用 mDiscoveryListener 侦听器。

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

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

你设法使它起作用了吗,在发现之前添加一个检查标志怎么样?

boolean isListenerUsed = false;

并且:

if (isListenerUsed == false) {
        isListenerUsed = true;
        mNsdManager.discoverServices(
                SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}