我想在公共類上使用靜態變量來在活動之間傳遞它們。如何在使用公共類上的靜態變量的活動之間傳遞數據?
我遇到了一個難得的問題。我給一個活動的靜態變量賦值。此活動調用GLSurfaceView並監聽屏幕方向更改。
如果我給GLSurfaceView上的靜態變量的值,那麼,所有工作正常,值存儲,我可以檢索他們,當屏幕方向更改後再次調用onCreate方法。
問題是當我在GLSurfaceView類之外的活動的onTouchListener方法內部存儲靜態變量的值時。這些值沒有正確存儲在靜態變量上,因爲當我嘗試在GLSurfaceView上訪問它們時,這些值不是它們應該是的。
這是我的靜態變量類:
public class MagazineStatus {
//clase utilizada para almacenar variables estáticas.
static int currentPage=1; //página actual
//Valores originales cuando pasamos de un modo a otro, por ejemplo, de portrait a landscape.
static float oScale=0.0f;
static float oX=0.0f;
static float oY=0.0f;
static float oZrot=0;
static boolean modeChanged=false; //indica si hemos cambiado de modo
(landscape/portrait)
}
這裏我存儲在我的活動類(我做了調試和aparently它們被正確地存儲)的值:
for (int i=0;i<thumbnailLinearLayouts.size();i++){
final int auxIndex=i;
thumbnailLinearLayouts.get(i).setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
MagazineStatus.currentPage=auxIndex;
System.out.println("MagazineStatus.currentPage: "+MagazineStatus.currentPage);
return true;
}
});
}
在這裏我試圖在GLSurfaceView類中檢索這些值,並且這些值不正確,它將檢索原始初始值1,而不是之前存儲的值。
currentPage=MagazineStatus.currentPage; //cargo datos guardados antes del cambio de orientación
我在做什麼錯了?
呵呵,你聲明一個變量final而不是重新賦值給它('auxIndex')。那裏發生了什麼? – 2012-03-09 16:49:22
工作,有沒有問題 – NullPointerException 2012-03-09 16:56:51
@elevine - 我錯了關於'最後'部分,每個這個問題+答案.n好的kow:http://stackoverflow.com/questions/609609/creating-final-變量 - 循環內 – 2012-03-09 19:58:47