gpt4 book ai didi

java - 在哪里实例化匿名类?

转载 作者:行者123 更新时间:2023-11-30 00:08:20 24 4
gpt4 key购买 nike

我正在尝试使用此链接中的代码 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(这个按钮将有 android:id="@+id/mybutton",这是你必须在 上使用的 id findViewById(R.id.mybutton).

在您的 Activity 的 onCreate 方法中,您将编写您向我们展示的代码,一切顺利。

又是一小步,如果你自己写了xml,一定要在onCreate中有这一行setContentView(R.layout.yourxml)

关于java - 在哪里实例化匿名类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48595429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com