我用GLSurfaceview做了一个3D程序,先让主ACTIVITY进入MAIN.XML ,在MAIN.XML上面放了一个按钮,点击按钮,进入GLSURFACEVIEW,在GLSURFACEVIEW中按BACK键,回到MAIN.XML ,但是我再按按钮,就不能进入GLSURFACEVIEW了。通过看LOG,按钮的响应函数没有被调用。
这个时候,如果旋转手机的方向,画面方向改变了,再点击按钮,又可以进入GLSURFACEVIEW了 奇怪 求组
主ACTIVITY代码如下:
- Java code
package Song.Hao;//import Song.Hao.NdkRender;import android.app.Activity;import android.os.Bundle;import android.opengl.GLSurfaceView;import android.util.DisplayMetrics;import android.util.Log;import android.view.KeyEvent;import android.view.MotionEvent;import android.view.GestureDetector; import android.view.View;import android.widget.Button;public class NDKOPENGLActivity extends Activity { /** Called when the activity is first created. */ private static final String TAG = "SongHao"; int width = 320; int height = 480; boolean move = false; float xpos = -1; float ypos = -1; public static float markX = -1; public static float markY = -1; // 0=up, 1=down, 2=right, 3=left boolean insurface = false; static int direction = -1; private GestureDetector mGestureDetector; private SoundPlayer m_sound; public GLSurfaceView surface; public NdkRender render; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); surface = new GLSurfaceView(this); render = new NdkRender(this); surface.setRenderer(render); DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); width = displayMetrics.widthPixels; height = displayMetrics.heightPixels; setContentView(R.layout.main); Button bt1 = (Button) findViewById(R.id.button1); m_sound.init(this); bt1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v ) { Log.i("songhao", " button1 clicked"); m_sound.startMusic(); insurface = true; jumptosurface(1); } }); // setContentView(surface); mGestureDetector = new GestureDetector(this, new MyGestureListener(this)); } public void jumptosurface(int type) { setContentView(surface); } public boolean onTouchEvent(MotionEvent me) { return mGestureDetector.onTouchEvent(me); } public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK) { if(insurface) { Log.i("songhao", "GOTO MAIN!"); m_sound.stopMusic(); surface.onPause(); setContentView(R.layout.main); insurface = false; } else System.exit(0); } //return super.onKeyDown(keyCode, event); return true; } static { System.loadLibrary("irrlicht"); System.loadLibrary("TestNdk"); }}
------解决方案--------------------
GL的东西还是和main.xml的东西分开在两个activity做吧。