当我编写处理程序并且编译器坚持将变量设置为最终变量时,为什么会收到此消息?
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();
});}
最佳答案
您必须将book、file、note 和Time 声明为final,因为您在内部使用它们匿名内部类。
我猜您不希望它们成为最终值,因为您想更改它们的值。这可以通过使用额外的最终变量来完成。如果你想改变书的值(value),你可以这样写:
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);
}
关于java - android处理程序将变量设置为final,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6558203/