Android 处理程序将变量设置为最终变量
当我编写处理程序并且编译器坚持将变量设置为最终变量时,为什么会收到此消息?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将book、file、note和Time声明为final,因为您在匿名内部类中使用它们。
我猜你不希望它们是最终的,因为你想改变它们的值。这可以通过使用额外的最终变量来完成。如果你想改变书的价值你可以这样写:
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: