我试图在公共类上使用静态变量来在活动之间传递它们。
我在这样做时遇到了一个罕见的问题。我正在为活动的静态变量赋予值。此活动调用 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
我做错了什么?