所以我试图使用youtube android 2D游戏教程中的一些代码。一个admob interstitial出现在我的setContentView(游戏);.有时,当我关闭测试广告时,我的屏幕会被冻结。完全没有反应。我甚至无法按下主页按钮退出。
我认为线程中可能存在问题,但它没有向我解释为什么onTouchEvents没有注册。如果无法避免这种情况,我至少想让我的用户有机会退出。我该如何解决这个问题?什么可能导致后退和主页按钮停止工作?
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class MainThread extends Thread
{
private int FPS = 30;
private double averageFPS;
private SurfaceHolder surfaceHolder;
private GamePanel gamePanel;
private boolean running;
public static Canvas canvas;
public MainThread(SurfaceHolder surfaceHolder, GamePanel gamePanel)
{
super();
this.surfaceHolder = surfaceHolder;
this.gamePanel = gamePanel;
}
@Override
public void run()
{
long startTime;
long timeMillis;
long waitTime;
long totalTime = 0;
int frameCount =0;
long targetTime = 1000/FPS;
while(running) {
startTime = System.nanoTime();
canvas = null;
//try locking the canvas for pixel editing
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
this.gamePanel.update();
this.gamePanel.draw(canvas);
}
} catch (Exception e) {
}
finally{
if(canvas!=null)
{
try {
surfaceHolder.unlockCanvasAndPost(canvas);
}
catch(Exception e){e.printStackTrace();}
}
}
timeMillis = (System.nanoTime() - startTime) / 1000000;
waitTime = targetTime-timeMillis;
try{
this.sleep(waitTime);
}catch(Exception e){}
totalTime += System.nanoTime()-startTime;
frameCount++;
if(frameCount == FPS)
{
averageFPS = 1000/((totalTime/frameCount)/1000000);
frameCount =0;
totalTime = 0;
System.out.println(averageFPS);
}
}
}
public void setRunning(boolean b)
{
running=b;
}
}
答案 0 :(得分:0)
我改变了一些东西,现在就可以了。老实说,我不知道发生了什么,但我很高兴现在没有发生。