2011-07-02 5 views
1

ハンドラを作成しているときにこのメッセージが表示され、コンパイラが変数を最終的なものにすることを主張しているのはなぜですか?アンドロイドハンドラが最終変数として

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

私の質問は、私は私のコードで非final変数を定義する方法です。(どのように私は非決勝にブック、ファイル、ノートや時間を変更することができます)、彼らはグローバル変数であり、私はそれらを渡しています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(); 
    });} 

答えて

1

値を変更したいので、最終的なものにしたくないと思います。これは追加の最終変数を使用することで達成できます。本の値を変更したい場合は、次のように書くことができます。

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); 
} 
+0

他の方法からの可変書類を渡していますか? – moe

関連する問題