【问题标题】:where to instantiate an anonymous class?在哪里实例化一个匿名类?
【发布时间】: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


【解决方案1】:

首先你需要一个包含一个按钮的xml(这个按钮将有android:id="@+id/mybutton",这就是你必须在findViewById(R.id.mybutton)上使用的id。

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

另一个小步骤,如果你自己写了xml,请确保在onCreate中有这一行 setContentView(R.layout.yourxml)

【讨论】:

  • 是的,很抱歉 :D 进行了编辑。是用我的手机写的
  • 谢谢。此行“public void initializeRegistrationListener()”、“;”中有错误预期的。但我不知道把“;”放在哪里确切地说,或者还有其他我看不到的错误。我编辑了我的帖子并将我的所有代码粘贴到那里。
  • 我试了你的activity类代码,没问题,没有预编译错误
猜你喜欢
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-30
  • 1970-01-01
  • 2021-04-27
相关资源
最近更新 更多