Android 处理程序将变量设置为最终变量

发布于 2024-11-18 02:25:33 字数 1043 浏览 1 评论 0原文

当我编写处理程序并且编译器坚持将变量设置为最终变量时,为什么会收到此消息?

Cannot refer to a non-final variable inside an inner class defined in a different 
method

我的问题是如何在代码中定义非最终变量:(如何将书籍、文件、注释和时间更改为非最终)它们是全局变量,我将它们传递给 trakingNotes

public void trakingNotes (final double book, final long file, final String note, final double Time) {
    MaxInfo = 100;
    Timer updateTimer = new Timer("NoteSaveings");
    updateTimer.scheduleAtFixedRate(new TimerTask() {
      public void run() {
       updateGUI( book, file, note, Time);
      }
    }, 0, 1000);
    }


NotesDB dbnotes = new NotesDB(this);

private void updateGUI( final double book, final long file, final String note, final double Time) {
     long idy;
      // Update the GUI
     noteHandler.post(new Runnable() {
    public void run() {
     long idy;
     dbnotes.open();     
     idy= dbnotes.insertnoteTitle (book, file, note, Time);
     if ((book) == samebook )//samebook is a global varible
     {ReadDBnotes();}
     dbnote.close();
  });}

Why do I get this message when I'm writing a handler and the compiler insist on making the variable as final?

Cannot refer to a non-final variable inside an inner class defined in a different 
method

My question is how can I define a non-final variable in my code: (how can I change book, file, note and time to non finals) they are global variables and I'm passing them to trakingNotes

public void trakingNotes (final double book, final long file, final String note, final double Time) {
    MaxInfo = 100;
    Timer updateTimer = new Timer("NoteSaveings");
    updateTimer.scheduleAtFixedRate(new TimerTask() {
      public void run() {
       updateGUI( book, file, note, Time);
      }
    }, 0, 1000);
    }


NotesDB dbnotes = new NotesDB(this);

private void updateGUI( final double book, final long file, final String note, final double Time) {
     long idy;
      // Update the GUI
     noteHandler.post(new Runnable() {
    public void run() {
     long idy;
     dbnotes.open();     
     idy= dbnotes.insertnoteTitle (book, file, note, Time);
     if ((book) == samebook )//samebook is a global varible
     {ReadDBnotes();}
     dbnote.close();
  });}

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

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

发布评论

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

评论(1

紫瑟鸿黎 2024-11-25 02:25:33

您必须将bookfilenoteTime声明为final,因为您在匿名内部类中使用它们。

我猜你不希望它们是最终的,因为你想改变它们的值。这可以通过使用额外的最终变量来完成。如果你想改变书的价值你可以这样写:

public void trakingNotes (double book, final long file, final String note, final double Time) {
    MaxInfo = 100;
    Timer updateTimer = new Timer("NoteSaveings");
    final double changedBook = book + 1.0;

    updateTimer.scheduleAtFixedRate(new TimerTask() {
      public void run() {
       updateGUI( changedBook, file, note, Time);
      }
    }, 0, 1000);
}

You have to declare book, file, note and Time as final because you use them inside of anonymous inner classes.

I guess you don't want them to be final because you want to change their values. This can be accomplished by using additional final variables. If you want to change the value of book you can write this:

public void trakingNotes (double book, final long file, final String note, final double Time) {
    MaxInfo = 100;
    Timer updateTimer = new Timer("NoteSaveings");
    final double changedBook = book + 1.0;

    updateTimer.scheduleAtFixedRate(new TimerTask() {
      public void run() {
       updateGUI( changedBook, file, note, Time);
      }
    }, 0, 1000);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文