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

时间:2015-09-06 15:36:06

标签: java android android-nsd

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

我正在尝试执行http://developer.android.com/training/connect-devices-wirelessly/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和Protocol,然后代码中有很多错误。这是正常的吗?这会导致崩溃吗?

2 个答案:

答案 0 :(得分:1)

问题似乎是由mDiscoveryListener方法调用引起的,您重新使用{{1}}侦听器。

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

  

listener ....不能为null。不能用于活动服务发现。

答案 1 :(得分:0)

您是否设法使其工作,以及如何在发现之前添加检查标志?

boolean isListenerUsed = false;

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