1

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

我正在尝试执行在http://developer.android.com/training/connect-devices-wireless/nsd.html#discover关于 NSD 的这个应用程序示例。当我执行它时,它说当我单击“发现”按钮时它无法执行活动的方法。所有其他按钮都没有问题。

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)

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

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 所在的 NsdManager.java 时,我看到很多错误,从导入开始说无法解析 SdkConstant 和 AsyncChannel 和协议,然后代码中出现很多错误。这是正常的吗?这是导致崩溃的原因吗?

4

2 回答 2

1

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

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

侦听器 .... 不能为空。不能用于活动服务发现。

于 2015-09-06T15:51:00.180 回答
0

你有没有设法让它工作,在发现之前添加一个检查标志怎么样?

boolean isListenerUsed = false;

和:

if (isListenerUsed == false) {
        isListenerUsed = true;
        mNsdManager.discoverServices(
                SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
于 2016-06-16T06:59:45.620 回答