我得到了示例项目BasicGLSurfaceView并对其进行了修改,以模拟我们项目中遇到的问题。
我修改了Activity以使用GLSurfaceView的SurfaceHolder的luckCanvas API:
public class BasicGLSurfaceViewActivity extends Activity implements OnClickListener { //Added a on click listener to draw some text on the view
......
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mView = new BasicGLSurfaceView(getApplication());
mView.setOnClickListener(this); // adding the on click listener
setContentView(mView);
}
.............
@Override
public void onClick(View arg0) {
Toast.makeText(this, "OnClicked", Toast.LENGTH_SHORT).show();//Toast message
AsyncTask asyncTask = new AsyncTask(){
@Override
protected Void doInBackground(Void... params) {
Canvas lockCanvas = mView.getHolder().lockCanvas(); // Trying to get canvas to draw something
if(lockCanvas!=null){
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(36.0f);
lockCanvas.drawText("Hello usgin locked canvas", 10, 50, paint);
mView.getHolder().unlockCanvasAndPost(lockCanvas);
}
return null;
}
};
asyncTask.execute();
}
}
如果检查此代码,您将看到lockCanvas始终为null,并且logcat中显示以下错误:
01-02 00:29:39.260: E/SurfaceHolder(1245): java.lang.IllegalArgumentException
01-02 00:29:39.260: E/SurfaceHolder(1245): at android.view.Surface.lockCanvasNative(Native Method)
01-02 00:29:39.260: E/SurfaceHolder(1245): at android.view.Surface.lockCanvas(Surface.java:76)
01-02 00:29:39.260: E/SurfaceHolder(1245): at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:771)
01-02 00:29:39.260: E/SurfaceHolder(1245): at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:747)
01-02 00:29:39.260: E/SurfaceHolder(1245): at com.example.android.basicglsurfaceview.BasicGLSurfaceViewActivity$1.doInBackground(BasicGLSurfaceViewActivity.java:61)
01-02 00:29:39.260: E/SurfaceHolder(1245): at com.example.android.basicglsurfaceview.BasicGLSurfaceViewActivity$1.doInBackground(BasicGLSurfaceViewActivity.java:1)
01-02 00:29:39.260: E/SurfaceHolder(1245): at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-02 00:29:39.260: E/SurfaceHolder(1245): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-02 00:29:39.260: E/SurfaceHolder(1245): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-02 00:29:39.260: E/SurfaceHolder(1245): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-02 00:29:39.260: E/SurfaceHolder(1245): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-02 00:29:39.260: E/SurfaceHolder(1245): at java.lang.Thread.run(Thread.java:856)
但是这个代码在GingerBread中没有任何问题 您能否解释一下Ice Cream Sandwich导致此问题的变化或解决此问题的解决方法是什么?
此外,我也收到以下错误:
12-21 10:04:47.254: E/EglHelper(17382): eglSwapBuffers returned EGL_BAD_NATIVE_WINDOW. tid=1507
12-21 10:04:47.285: E/AndroidRuntime(17382): FATAL EXCEPTION: GLThread 1507
12-21 10:04:47.285: E/AndroidRuntime(17382): java.lang.RuntimeException: eglSwapBuffers failed: EGL_BAD_SURFACE
12-21 10:04:47.285: E/AndroidRuntime(17382): at android.opengl.GLSurfaceView$EglHelper.throwEglException(GLSurfaceView.java:1178)
12-21 10:04:47.285: E/AndroidRuntime(17382): at android.opengl.GLSurfaceView$EglHelper.swap(GLSurfaceView.java:1136)
12-21 10:04:47.285: E/AndroidRuntime(17382): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1463)
12-21 10:04:47.285: E/AndroidRuntime(17382): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1216)
非常感谢。