我的Android游戏出现问题。当我进入活动时,它工作正常,但是当我离开它,然后尝试重新进入或按下手机上的“后退”(硬件)按钮时,应用程序会说:“应用程序无响应”。
我是Android编程的新手,我很难理解错误信息,你能帮帮我吗?
修改
LOGCAT中的错误
07-31 17:17:22.166: E/ActivityManager(344): ANR in com.coderogden.pongtennis (com.coderogden.pongtennis/.activities.StartLvlActivity)
07-31 17:17:22.166: E/ActivityManager(344): Reason: keyDispatchingTimedOut
07-31 17:17:22.166: E/ActivityManager(344): Load: 3.58 / 1.22 / 1.46
07-31 17:17:22.166: E/ActivityManager(344): CPU usage from 4577ms to -2827ms ago:
07-31 17:17:22.166: E/ActivityManager(344): 30% 344/system_server: 24% user + 6% kernel / faults: 1270 minor 55 major
07-31 17:17:22.166: E/ActivityManager(344): 7.4% 482/com.android.systemui: 6.3% user + 1% kernel / faults: 406 minor 7 major
07-31 17:17:22.166: E/ActivityManager(344): 3.4% 23661/com.google.process.location: 2.8% user + 0.6% kernel / faults: 1619 minor 52 major
07-31 17:17:22.166: E/ActivityManager(344): 1.5% 26194/com.android.vending: 0.8% user + 0.7% kernel / faults: 1555 minor 47 major
07-31 17:17:22.166: E/ActivityManager(344): 2.9% 142/surfaceflinger: 2% user + 0.9% kernel / faults: 1 minor
07-31 17:17:22.166: E/ActivityManager(344): 2.8% 25/kswapd0: 0% user + 2.8% kernel
07-31 17:17:22.166: E/ActivityManager(344): 1.8% 571/com.android.phone: 1.2% user + 0.6% kernel / faults: 411 minor 12 major
07-31 17:17:22.166: E/ActivityManager(344): 1.4% 26085/com.coderogden.pongtennis: 0.8% user + 0.6% kernel / faults: 382 minor 26 major
07-31 17:17:22.166: E/ActivityManager(344): 1.2% 25918/kworker/0:2: 0% user + 1.2% kernel
07-31 17:17:22.166: E/ActivityManager(344): 0.9% 88/mmcqd/0: 0% user + 0.9% kernel
07-31 17:17:22.166: E/ActivityManager(344): 0.8% 150/sensord: 0.1% user + 0.6% kernel
07-31 17:17:22.166: E/ActivityManager(344): 0.5% 145/mediaserver: 0.1% user + 0.4% kernel
07-31 17:17:22.166: E/ActivityManager(344): 0.5% 19146/com.google.process.gapps: 0.4% user + 0.1% kernel / faults: 291 minor 5 major
07-31 17:17:22.166: E/ActivityManager(344): 0.5% 24764/kworker/u:3: 0% user + 0.5% kernel
07-31 17:17:22.166: E/ActivityManager(344): 0.5% 24950/com.google.android.apps.plus: 0.5% user + 0% kernel / faults: 44 minor
07-31 17:17:22.166: E/ActivityManager(344): 0.5% 25950/kworker/u:2: 0% user + 0.5% kernel
07-31 17:17:22.166: E/ActivityManager(344): 0.4% 158/adbd: 0% user + 0.4% kernel
07-31 17:17:22.166: E/ActivityManager(344): 0% 588/com.lge.lgfotaclient: 0% user + 0% kernel / faults: 624 minor
和
07-31 17:17:19.275: I/dalvikvm(26085): threadid=3: reacting to signal 3
07-31 17:17:19.447: I/dalvikvm(26085): Wrote stack traces to '/data/anr/traces.txt'
07-31 17:17:51.564: E/Trace(26401): error opening trace file: No such file or directory (2)
07-31 17:17:51.588: V/ActivityThread(26401): Class path: /data/app /com.coderogden.pongtennis-1.apk, JNI path: /data/data/com.coderogden.pongtennis/lib
07-31 17:17:51.752: I/dalvikvm-heap(26401): Grow heap (frag case) to 12.517MB for 700016-byte allocation
07-31 17:17:51.783: D/dalvikvm(26401): GC_CONCURRENT freed 828K, 18% free 10384K/12651K, paused 7ms+2ms, total 25ms
07-31 17:17:51.853: I/dalvikvm-heap(26401): Grow heap (frag case) to 14.213MB for 700016-byte allocation
07-31 17:17:51.908: I/dalvikvm-heap(26401): Grow heap (frag case) to 15.883MB for 700016-byte allocation
07-31 17:17:51.947: I/dalvikvm-heap(26401): Grow heap (frag case) to 18.085MB for 700016-byte allocation
07-31 17:17:52.056: D/libEGL(26401): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
07-31 17:17:52.088: D/libEGL(26401): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
07-31 17:17:52.088: D/libEGL(26401): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
07-31 17:17:52.189: D/OpenGLRenderer(26401): Enabling debug mode 0
错误的重要JAVA代码
MAIN THREAD
@Override
public void run() {
Canvas canvas;
Log.d(TAG, "Starting game loop");
long beginTime; // The time when the cycle begun
long timeDiff; // The time it took for the cycle to execute
int sleepTime; // ms to sleep (<0 if we're behind)
int framesSkipped; // Number of frames being skipped
sleepTime = 0;
while (running) {
canvas = null;
// Try locking the canvas
try {
canvas = this.surfaceHolder.lockCanvas();
if (canvas != null) {
synchronized (surfaceHolder) {
beginTime = System.currentTimeMillis();
framesSkipped = 0; // resetting the frames skipped
// Update game state here!
this.gameView.update();
// Render state to the screen
// Draws the canvas on the panel
this.gameView.render(canvas);
// Calculate how long time the cycle took
timeDiff = System.currentTimeMillis() - beginTime;
// Calculate sleep time
sleepTime = (int) (FRAME_PERIOD - timeDiff);
if (sleepTime > 0) {
try {
// Send the thread to sleep for a short period,
// very useful for battery saving
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
}
}
while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
// Need to catch up by updating without rendering
// Update game state here!
this.gameView.update();
// Add frame period to check if in next frame
sleepTime += FRAME_PERIOD;
framesSkipped++;
}
}
}
} finally {
// In case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
} // End finally
}
SURFACEVIEW-CLASS
// //////////////////////////////////////////////////////////////////////////////////
// The render() method renders the UI graphics on the screen
public void render(Canvas canvas) {
super.onDraw(canvas);
if (playing) {
// Draw components
box.draw(canvas);
// Draw booster/s
if (racketTouches > 4) {
if (b1.isUsed()) {
b1 = new Booster();
}
canvas.drawBitmap(b1.booster, b1.boosterX, b1.boosterY, null);
}
if (racketTouches > 14) {
if (b2.isUsed()) {
b2 = new Booster();
}
canvas.drawBitmap(b2.booster, b2.boosterX, b2.boosterY, null);
}
if (racketTouches > 24) {
if (b3.isUsed()) {
b3 = new Booster();
}
canvas.drawBitmap(b3.booster, b3.boosterX, b3.boosterY, null);
}
// Draw rackets and ball
player.draw(canvas);
computer.draw(canvas);
ball.draw(canvas);
} else {
// Draw components
box.draw(canvas);
player.draw(canvas);
computer.draw(canvas);
ball.draw(canvas);
}
}
答案 0 :(得分:1)
当您的应用在其主(UI)线程上执行太多操作时,会发生ANR。检查您的代码(Activity
,Service
或BroadcastReceiver
),确保您没有进行长时间运行或阻止操作。如果你的StartLvlActivity
试图锁定表面并渲染它,请连接到网络,或者执行其他类型的“昂贵”#34;然后你需要把它移到另一个线程。在宣布ANR之前,系统会给你大约5秒钟(永恒!),所以你的代码中显然会有很长时间的运行。
有关Android中进程和线程的详细信息,请参阅此Android developer page。