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

标签 java android anonymous-class

我正在尝试使用此链接中的代码 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/

相关文章:

php - Android 应用程序并登录我的数据库

java - 从内部匿名 Runnable 访问外部变量

java - 在 Selenium getattribute() 中同时使用文本和位置

java - 生成概率树然后对结果进行排序的时间高效实现

java - JCombobox 无法将多个选项重命名为相同的值

java - 从原始值键值对 hashmap Java 中获取键值对

java - 获取图像中最常出现的颜色

java - 为什么这段 Java 代码有效?编译器不会提示关闭

c++ - 使用 typedef 和匿名类定义与传统类定义的等效性

java - 我正在尝试找到一种方法来处理 key '1' 的重复条目 'movieID' 的错误 1062