我的应用程序有一个非常奇怪的问题!
我一直在bluestacks上编程,一切都很好 - 没有崩溃或者什么都没有。当我将apk移动到我的手机时它也工作正常直到我关闭应用程序 - 这导致了崩溃,我不知道如何获取。
这让我怀疑我的问题与我正在使用的SurfaceView有关,但我不知道导致崩溃的问题可能是什么。
这里是onCreate:
GameView g;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
g=new GameView(this);
setContentView(g);
}
以下是我创建surfaceview的方法:
gameLoopThread = new GameLoopThread(this);
this.requestFocus();
this.setFocusableInTouchMode(true);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
}
,这是主题:
import android.annotation.SuppressLint;
import android.graphics.Canvas;
public class GameLoopThread extends Thread {
static final long FPS = 60;
private GameView view;
private boolean running = false;
public GameLoopThread(GameView view) {
this.view = view;
}
public void setRunning(boolean run) {
running = run;
}
@SuppressLint("WrongCall") @Override
public void run() {
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (running) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
try {
//if (sleepTime > 0)
// sleep(sleepTime);
// else
//sleep(10);
} catch (Exception e) {}
}
}
}
我没有触及ondestroy或onstop,所以问题必须出在我怀疑的surfaceview中。
编辑: 整个游戏视图:
public GameView(Context c) {
// TODO Auto-generated constructor stub
super(c);
this.c = c;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
ScoreParticleP=new PointF();
NewScoreParticleP=new PointF();
int srcWidth = options.outWidth;
int srcHeight = options.outHeight;
//it=blocks.iterator();
// Decode with inSampleSize
options.inJustDecodeBounds = false;
options.inDither = false;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
this.setKeepScreenOn(true);
WindowManager wm = (WindowManager) c
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
this.screenw = display.getWidth();
this.screenh = display.getHeight();
this.differencew = (double) screenw / normalw;
this.differenceh = (double) screenh / normalh;
mediaPlayer = MediaPlayer.create(c, R.raw.nyan);
mediaPlayer.setLooping(true);
mediaPlayer.start();
mediaPlayer2 = MediaPlayer.create(c, R.raw.remix);
mediaPlayer2.setLooping(true);
mediaPlayer3 = MediaPlayer.create(c, R.raw.weed);
mediaPlayer3.setLooping(true);
SharedPreferences prefs2 = c.getSharedPreferences("Sp.game.spiceinspace", Context.MODE_PRIVATE);
counter2=prefs2.getInt("score", 0);
this.sprite = BitmapFactory.decodeResource(getResources(),
R.drawable.sprite,options);
this.sprite = Bitmap.createScaledBitmap(sprite, sprite.getWidth() * 3,
sprite.getHeight() * 3, false);
this.heart=BitmapFactory.decodeResource(getResources(),
R.drawable.heart);
this.heart=Bitmap.createScaledBitmap(heart, heart.getWidth() * 3,
heart.getHeight() * 3, false);
currentSpeed = new PointF(0, 0);
currentDirection = new Point(0, 0);
currentPosition = new Point(350, 350);
this.background = BitmapFactory.decodeResource(getResources(),
R.drawable.space);
this.background=Bitmap.createScaledBitmap(background, background.getWidth()*5, background.getHeight()*5, false);
this.lost= BitmapFactory.decodeResource(getResources(),
R.drawable.gameover);
this.lostNew= BitmapFactory.decodeResource(getResources(),
R.drawable.gameovernew);
lostNew=FitAllDevices(lostNew);
lost=FitAllDevices(lost);
this.alien = BitmapFactory.decodeResource(getResources(),
R.drawable.mob_alien);
this.coin = BitmapFactory.decodeResource(getResources(),
R.drawable.item_coin);
partic=BitmapFactory.decodeResource(getResources(),
R.drawable.particle_star);
partic=Bitmap.createScaledBitmap(partic, partic.getWidth()*2, partic.getHeight()*2, false);
this.alien = Bitmap.createScaledBitmap(alien, alien.getWidth() * 3,
alien.getHeight() * 3, false);
asteroid=BitmapFactory.decodeResource(getResources(),
R.drawable.mob_astroid);
asteroid=Bitmap.createScaledBitmap(asteroid, asteroid.getWidth() * 3,
asteroid.getHeight() * 3, false);
goldasteroid=BitmapFactory.decodeResource(getResources(),
R.drawable.mob_goldastroid);
goldasteroid=Bitmap.createScaledBitmap(goldasteroid, goldasteroid.getWidth() * 3,
goldasteroid.getHeight() * 3, false);
mushroom=BitmapFactory.decodeResource(getResources(),
R.drawable.item_mushroom);
mushroom=Bitmap.createScaledBitmap(mushroom, mushroom.getWidth() * 4,
mushroom.getHeight() * 4, false);
coin=Bitmap.createScaledBitmap(coin, coin.getWidth() * 2,
coin.getHeight() * 2, false);
drug=BitmapFactory.decodeResource(getResources(),
R.drawable.item_not);
drug=Bitmap.createScaledBitmap(drug, drug.getWidth() * 4,
drug.getHeight() * 4, false);
rocket=BitmapFactory.decodeResource(getResources(),
R.drawable.item_rocket);
rocket=Bitmap.createScaledBitmap(rocket, rocket.getWidth() * 4,
rocket.getHeight() * 4, false);
alien = FitAllDevices(alien);
mushroom = FitAllDevices(mushroom);
drug = FitAllDevices(drug);
rocket = FitAllDevices(rocket);
asteroid=FitAllDevices(asteroid);
goldasteroid=FitAllDevices(goldasteroid);
sprite = FitAllDevices(sprite);
heart=FitAllDevices(heart);
player = new Spicy(sprite,heart);
hit= soundPool.load(c, R.raw.hit, 1);
pass= soundPool.load(c, R.raw.win, 1);
remix= soundPool.load(c, R.raw.remix, 1);
destroy= soundPool.load(c, R.raw.destroy, 1);
aliensound= soundPool.load(c, R.raw.alien, 1);
gameLoopThread = new GameLoopThread(this);
this.requestFocus();
this.setFocusableInTouchMode(true);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
}
编辑2 我找到了手机的logcat,这里是崩溃:
答案 0 :(得分:1)
根据logcat输出的屏幕抓取,您从java.lang.Thread.stop()获得UnsupportedOperationException。这是所有Android版本的行为,因为该调用已被弃用,除了抛出异常之外什么都不做。
从您的代码(Sp.game.spiceinspace.MainActivity.onStop
,MainActivity.java第26行)调用它。你需要停止这样做,并以其他方式结束线程。
应用程序可能也会在模拟器上崩溃,但出于某种原因,崩溃并未引起您的注意。