我正在开发一个简单的Android游戏应用程序。在我的主要活动(用于播放的活动)中,当一个条件被验证时,在1/2秒后,我希望停止当前的活动并重新启动它。游戏有时运行良好,但通常是随机的,它崩溃“没有错误”-它没有重新启动活动,但它关闭了视图返回到游戏菜单活动(用于菜单,选项等的活动,等等)。
以下是问题发生时的代码段:
if(scoreManager.isEndGame()){
final Handler mHandler = new Handler();
Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long remainingTime = scoreManager.getSecondsUntilFinished();
scoreManager.setRemainingTime(remainingTime*1000);
finish();
startActivity(getIntent());
}
};
mHandler.postDelayed(mUpdateTimeTask, 500);
}scoreManager包含一个在每次重新启动时与remainingTime连接的CountDownTimer (它在onCreate()上实例化)。
我在LogCat上读到:
InputMethodManagerService : RemoteException向pid 18494 uid 10062发送setActive(假)通知 I/ActivityManager :显示com.myproject.Activity/..MenuActivity:+774(总计+3s697ms)
基本上,活动经常被摧毁,而且不会重新启动。不抛出任何例外。我有这个问题测试我的"HTC愿望“。
请帮帮忙。
发布于 2013-03-28 00:40:55
解决了!
最后我发现了问题,它是关于"onCreate“方法的主要活动。试图随机设置背景图像,有时所选图像的分辨率太大(R.drawable.background_3和R.drawable.background_5)过大( 1024x768或更高),因此主活动被中断并返回到MenuActivity。我解决了将所有背景图像的分辨率设置为640x480的问题。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
//stuff...
//set background
LinearLayout linLay = (LinearLayout) findViewById(R.id.mainLayout);
linLay.setBackgroundResource(getBackgroundCode(new Random().nextInt(12)));
//other stuff...
}catch(Exception e){
Log.e("MainPlayActivity", "onCreate() - Error during cretion. Exception: "+e.getMessage(), e);
}
}
private int getBackgroundCode(int n){
int result=0;
switch (n){
case 0:
result = R.drawable.background_0;
break;
case 1:
result = R.drawable.background_1;
break;
case 2:
result = R.drawable.background_2;
break;
case 3:
result = R.drawable.background_3;
break;
case 4:
result = R.drawable.background_4;
break;
case 5:
result = R.drawable.background_5;
break;
case 6:
result = R.drawable.background_6;
break;
case 7:
result = R.drawable.background_7;
break;
case 8:
result = R.drawable.background_8;
break;
case 9:
result = R.drawable.background_9;
break;
case 10:
result = R.drawable.background_10;
break;
case 11:
result = R.drawable.background_11;
break;
default :
result = R.drawable.background_0;
}
return result;
}https://stackoverflow.com/questions/15591576
复制相似问题