Here is a pseudo code :
class MoWeFi extends Activitystatic TouchSurfaceView TSV; static DrawRenderer mRenderer; static UserInterface mUI; DownloadData mDD = new DownloadData(); static float zoom = 1.f; mDD.execute(); mUI = new UserInterface(); setContentView(mUI);class UserInterface extends LinearLayout
MoWeFi.TSV = new TouchSurfaceView(context);
MoWeFi.mRenderer = new DrawRenderer(context);
MoWeFi.TSV.setRenderer(MoWeFi.mRenderer);
MoWeFi.TSV.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
this.addView(MoWeFi.TSV);TextView tv = new TextView(context); this.addView(tv); TextView tv2 = new TextView(context); this.addView(tv2);class DownloadData extends AsyncTask
protected Void doInBackground(){
do{
this.publishProgress();
try{Thread.sleep(1000);catch (InterruptedException e){}
}while(running)
}
@Override
protected void onProgressUpdate(){
//...
MoWeFi.TSV.requestRender();
MoWeFi.mUI.tv.setText("zoom="+MoWeFi.zoom); // IT DOES NOT WORK !!! ALWAYS GET 1.f.
}class TouchSurfaceView extends GLSurfaceView
gl.glTranslatef(0, 0, -1.f / MoWeFi.zoom); // IT WORKS !!!class DrawRenderer implements GLSurfaceView.Renderer
//Pinch-zoom
...
MoWeFi.zoom *= detector.getScaleFactor();
MoWeFi.zoom = Math.max(0.5f, Math.min(MoWeFi.zoom, 5.0f));
MoWeFi.mUI.tv2.setText("zoom="+MoWeFi.zoom); // IT WORKS !!!
So no error whatsoever. Just not able to access my zoom variable everywhere.
I am new to Java, I am surely missing important concept.EDIT add float in the first lines
EDIT 2 add publishProgress in the asyncTask
EDIT 3 logcat shows nothing
Are you sure your
AsyncTaskaccesses the property after the value was changed? If yes,AsyncTaskruns code on a separate thread so probably you’re facing a race condition where the value updated in one thread is not yet available in another. Try to make thezoomproperty volatile:or use synchronized get/set methods to mutate and access the property.