【发布时间】:2018-02-03 08:27:23
【问题描述】:
我正在尝试使用此链接中的代码https://developer.android.com/training/connect-devices-wirelessly/nsd.html
“来自网络上的发现服务。” 我复制并粘贴代码如下:
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button aButton = (Button) findViewById(R.id.MyButton);
aButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// there is an error in the next line, ";" expected.
// but I do not know where to put ; exactly
public void initializeRegistrationListener() {
mRegistrationListener = new NsdManager.RegistrationListener() {
@Override
public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
// Save the service name. Android may have changed it in order to
// resolve a conflict, so update the name you initially requested
// with the name Android actually used.
mServiceName = NsdServiceInfo.getServiceName();
}
@Override
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
// Registration failed! Put debugging code here to determine why.
}
@Override
public void onServiceUnregistered(NsdServiceInfo arg0) {
// Service has been unregistered. This only happens when you call
// NsdManager.unregisterService() and pass in this listener.
}
@Override
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
// Unregistration failed. Put debugging code here to determine why.
}
};
} }
});
}
}
但是这行"public void initializeRegistrationListener()", ";" 有错误预期的。但我不知道把“;”放在哪里到底是什么,或者有什么我看不到的错误,请有人指导我吗?
PS:我试图让我的手机发现我在笔记本电脑上使用 javascript 创建的 Mdns 服务,我没有 Java 经验,但我需要运行以前的代码来测试我已经创建的服务。
【问题讨论】:
-
这不起作用这是什么意思?
-
首先在xml中添加一个按钮然后启动它,然后在oncreate中添加这个代码..解释清楚你在尝试什么......
-
@Ravi 这行给我一个错误 public void initializeDiscoveryListener() {
-
@MLN 我添加了按钮,你的意思是我需要将“public void onClick(View v){”更改为“public void oncreate(View v){”
-
我正在尝试让我的手机发现我在笔记本电脑上使用 javascript 创建的 Mdns 服务,我没有使用 java 的经验,但我需要运行之前的代码来测试我已经创建的服务.
标签: java android anonymous-class