应用程序在恢复后没有响应

时间:2011-11-08 18:45:05

标签: java android android-anr-dialog

我的'游戏'在我恢复之后不断崩溃。基本上,我启动应用程序,一切正常。然后我按下主页按钮返回主屏幕。仍然是所有正常的东西。但是,当我打开应用程序时,它只是冻结,大约1分钟后,我收到一条无响应的消息。

这是我的主要活动:

package com.amzoft.android.starraider;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;

public class StarRaiderMain extends Activity implements OnTouchListener, OnKeyListener, SensorEventListener{

FastRenderView renderView;
SensorManager sensorManager;
Sensor accelerometer;
Thread mainGameThread;

Player player;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    renderView = new FastRenderView(this);
    renderView.setOnKeyListener(this);
    renderView.setFocusableInTouchMode(true);
    renderView.requestFocus();
    setContentView(renderView);

    sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    accelerometer = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);

    mainGameThread = new Thread(new mainGameThread());

    player = new Player(this);
}

@Override
protected void onResume()
{
    super.onResume();
    renderView.resume();
    renderView.requestFocus();
    sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onPause()
{
    super.onPause();
    renderView.pause();
    sensorManager.unregisterListener(this);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) 
{

}

@Override
public void onSensorChanged(SensorEvent event) 
{
    player.onSensorChanged(event);
}

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) 
{
    player.onKey(v, keyCode, event);
    return true;
}

@Override
public boolean onTouch(View v, MotionEvent event) 
{

    return true;
}

class FastRenderView extends SurfaceView implements Runnable
{
    Thread renderThread = null;
    SurfaceHolder holder;
    volatile boolean running = false;

    public FastRenderView(Context context)
    {
        super(context);
        holder = getHolder();
    }

    public void resume()
    {
        running = true;
        renderThread = new Thread(this);
        renderThread.start();
    }

    public void pause()
    {
        running = false;
        while(true)
        {
            try{
                renderThread.join();
            }catch(InterruptedException e){
                Log.d("StarRaider", e.getMessage());
            }
        }
    }

    @Override
    public void run()
    {
        while(running)
        {
            if(!holder.getSurface().isValid())
                continue;
            Canvas canvas = holder.lockCanvas();
            onDraw(canvas);
            holder.unlockCanvasAndPost(canvas);
        }
    }

    public void onDraw(Canvas canvas)
    {
        canvas.drawRGB(255, 255, 255);
        player.onDraw(canvas);
    }
}
class mainGameThread extends Thread
{
    mainGameThread()
    {
        super("mainGameThread");
        start();
    }
    public void run()
    {
        try{
            player.onUpdate();
        }catch(Exception e){

        }
        }
    }
}

screenie

非常感谢任何帮助。

这是我的logcat输出:http://pastebin.com/3b09YAkP 我有一个主游戏线程,因为我读到如果渲染线程卡住2秒钟,它会崩溃。

2 个答案:

答案 0 :(得分:1)

看起来它可能是两件事之一。

您可能应该在onPause()中明确暂停您的主题。如果它试图在没有暂停的情况下恢复,那么这可能会导致应用程序死锁。

您还应该查看Replica Island上的Google I / O演示文稿。大约一个小时。记下他谈论垃圾收集的部分。这可能是正在发生的事情并导致您的应用因ANR错误而崩溃。

我希望这有帮助!

答案 1 :(得分:0)