如何在匿名类中引用更高的类

发布于 2024-08-20 18:24:39 字数 669 浏览 7 评论 0原文

我有这个代码:

public class Home extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            //...
            //at some point I have
            s.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

              @Override
              public void onProgressChanged(SeekBar seekBar, int progress,
                      boolean fromUser) {

                  ContextNotionLevel ctnl=new ContextNotionLevel(this);
// <-- how can I reference Home class here to replace **this**, which as it is points to OnSeekBarChangeListener
              }
    }
}

I have this code:

public class Home extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            //...
            //at some point I have
            s.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

              @Override
              public void onProgressChanged(SeekBar seekBar, int progress,
                      boolean fromUser) {

                  ContextNotionLevel ctnl=new ContextNotionLevel(this);
// <-- how can I reference Home class here to replace **this**, which as it is points to OnSeekBarChangeListener
              }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半仙 2024-08-27 18:24:39

您可以尝试:

 ContextNotionLevel ctnl=new ContextNotionLevel(Home.this);

You can try:

 ContextNotionLevel ctnl=new ContextNotionLevel(Home.this);
凤舞天涯 2024-08-27 18:24:39

您可以使用 Home.this 来引用 Home 对象。

You can use Home.this to refer to the Home object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文