我正在使用自定义 SurfaceView 编写一个小游戏。我想在 SurfaceView 的底部放置一个 AdMob 视图,但在运行该应用程序大约六分钟后,该应用程序抛出一个错误,然后重新启动手机。我已经用一个按钮替换了 AdMob 视图,但错误仍然存在,所以我认为问题是由于将任何视图放在 SurfaceView 上引起的。如果我从 SurfaceView 顶部删除任何视图,游戏将无限期地运行良好。
我尝试使用除RelativeLayout 以外的布局,但问题仍然存在。我想知道是否有人以前遇到过这种情况,或者有任何其他途径可以探索。
自定义 SurfaceView:
public class AndroidFastRenderView extends SurfaceView implements Runnable
{
private static final String TAG = "AndroidFastRenderView";
AndroidGame game;
Bitmap framebuffer;
Thread renderThread = null;
SurfaceHolder holder;
volatile boolean running = false;
public AndroidFastRenderView(AndroidGame game, Bitmap framebuffer)
{
super(game);
this.game = game;
this.framebuffer = framebuffer;
this.holder = getHolder();
}
public AndroidFastRenderView(Context context)
{
super(context);
}
public void resume()
{
running = true;
renderThread = new Thread(this);
renderThread.start();
}
public void run()
{
Rect dstRect = new Rect(0,0,80,120);
long startTime = System.nanoTime();
while (running)
{
if(!holder.getSurface().isValid())
{
continue;
}
float deltaTime = (System.nanoTime() - startTime) / 1000000000.0f;
startTime = System.nanoTime();
game.getCurrentScreen().update(deltaTime);
game.getCurrentScreen().present(deltaTime);
Canvas canvas = null;
canvas = holder.lockCanvas();
if (canvas != null)
{
dstRect = canvas.getClipBounds();
canvas.drawBitmap(framebuffer, null, dstRect, null);
holder.unlockCanvasAndPost(canvas);
}
}
}
public void pause()
{
Log.e(TAG, "Paused");
running = false;
while(true)
{
try {
renderThread.join();
break;
} catch (InterruptedException e) {
// retry
}
}
}
}
在主要活动中构建界面:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
int frameBufferWidth = isLandscape ? 120 : 80;
int frameBufferHeight = isLandscape ? 80 : 120;
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Config.RGB_565);
float scaleX = (float) frameBufferWidth / getWindowManager().getDefaultDisplay().getWidth();
float scaleY = (float) frameBufferHeight / getWindowManager().getDefaultDisplay().getHeight();
renderView = new AndroidFastRenderView(this, frameBuffer);
graphics = new AndroidGraphics(getAssets(), frameBuffer);
fileIO = new AndroidFileIO(getAssets());
audio = new AndroidAudio(this);
input = new AndroidInput(this, renderView, scaleX, scaleY);
screen = this.getStartScreen();
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
if (useAds)
{
//adView = new AdView(this, AdSize.SMART_BANNER, AD_UNIT_ID);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
//adView.setLayoutParams(params);
RelativeLayout layout = new RelativeLayout(this);
layout.addView(renderView);
//layout.addView(adView);
Button button = new Button(this);
button.setLayoutParams(params);
button.setText("Blargle");
layout.addView(button);
//adView.loadAd(new AdRequest());
setContentView(layout);
}
else
{
setContentView(renderView);
}
}
错误的LogCat:
01-25 13:50:18.084: E/SurfaceTextureClient(10342): dequeueBuffer failed (Broken pipe)
01-25 13:50:18.084: E/SurfaceHolder(10342): Exception locking surface
01-25 13:50:18.084: E/SurfaceHolder(10342): java.lang.IllegalArgumentException
01-25 13:50:18.084: E/SurfaceHolder(10342): at android.view.Surface.lockCanvasNative(Native Method)
01-25 13:50:18.084: E/SurfaceHolder(10342): at android.view.Surface.lockCanvas(Surface.java:88)
01-25 13:50:18.084: E/SurfaceHolder(10342): at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:754)
01-25 13:50:18.084: E/SurfaceHolder(10342): at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:734)
01-25 13:50:18.084: E/SurfaceHolder(10342): at com.somethingutility.games.framework.impl.AndroidFastRenderView.run(AndroidFastRenderView.java:60)
01-25 13:50:18.084: E/SurfaceHolder(10342): at java.lang.Thread.run(Thread.java:856)
此错误会一直重复,直到手机重新启动。