我正在尝试在公共类上使用静态变量在活动之间传递它们。
我在这样做时遇到了一个罕见的问题。我正在为活动中的静态变量赋值。此活动调用 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)
}
在这里,我将值存储在我的活动类中(我进行了调试,显然它们被正确存储):
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
我做错了什么?