Java Android应用程序没有响应

Java Android应用程序没有响应,java,android,Java,Android,我的Android游戏有问题。当我输入一个活动时,它工作正常,但当我离开它,然后尝试重新输入或按下手机上的“后退”硬件按钮时,应用程序会显示:应用程序无响应 我是Android编程新手,我不善于理解错误信息,你能帮我吗 编辑 LOGCAT中的错误 及 错误的重要JAVA代码 主线 @Override public void run() { Canvas canvas; Log.d(TAG, "Starting game loop"); long beginTime; /

我的Android游戏有问题。当我输入一个活动时,它工作正常,但当我离开它,然后尝试重新输入或按下手机上的“后退”硬件按钮时,应用程序会显示:应用程序无响应

我是Android编程新手,我不善于理解错误信息,你能帮我吗

编辑

LOGCAT中的错误

错误的重要JAVA代码

主线

@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
    }

当应用程序在其主UI线程上执行太多操作时,就会发生ANR。检查您的代码活动、服务或广播接收器,以确保您没有执行长时间运行或阻塞操作。如果您的活动试图锁定曲面并对其进行渲染、连接到网络或执行其他类型的昂贵操作,则需要将其移动到另一个线程。系统会给你5秒的时间!在声明ANR之前,显然代码中存在一些长期运行的问题


有关Android中进程和线程的更多详细信息,请参阅此Android。

能否尝试在生命周期方法中使用断点进行调试,以查看其功能?要调试代码,我们需要查看相关代码和完整的logcat输出。我会告诉你关于logcat的内容以及如何阅读。我编辑了我的帖子,你现在能看到什么吗?
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
@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
    }
// //////////////////////////////////////////////////////////////////////////////////
// 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);
    }
}