SurfaceHolder(活动崩溃)

时间:2015-04-15 14:44:22

标签: android

我有一个问题,它是:我转到活动,然后返回菜单,然后我再次尝试从菜单中转到相同的活动。在第二次尝试中,我得到一个错误“内存不足”,并且在mCallbacksRunning = true之后,Choreographer.class在箭头处打开。 }

void doCallbacks(int callbackType, long frameTimeNanos) {
        CallbackRecord callbacks;
        synchronized (mLock) {
            // We use "now" to determine when callbacks become due because it's possible
            // for earlier processing phases in a frame to post callbacks that should run
            // in a following phase, such as an input event that causes an animation to start.
            final long now = SystemClock.uptimeMillis();
            callbacks = mCallbackQueues[callbackType].extractDueCallbacksLocked(now);
            if (callbacks == null) {
                return;
            }
            mCallbacksRunning = true;
        }
        try {
            for (CallbackRecord c = callbacks; c != null; c = c.next) {
                if (DEBUG) {
                    Log.d(TAG, "RunCallback: type=" + callbackType
                            + ", action=" + c.action + ", token=" + c.token
                            + ", latencyMillis=" + (SystemClock.uptimeMillis() - c.dueTime));
                }
                c.run(frameTimeNanos);
            }

活动的简短代码:

public class Test extends SurfaceView implements SurfaceHolder.Callback{
public Test(Context context) {
 super(context);
 getHolder().addCallback(this);
}

         public void surfaceCreated(SurfaceHolder holder) { 
     updateThread = new UT(this);
     updateThread.setRunning(true);
     updateThread.start();
     }

     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
     }

     public void surfaceDestroyed(SurfaceHolder holder) {    
     boolean retry = true;
     updateThread.setRunning(false);
     while (retry) {
     try {updateThread.join();
     retry = false; } 
     catch (InterruptedException e) {
     }
     }
     }
    }

更新Tread:

public class UT extends Thread {

 private long time;
 private final int fps = 20;
 private boolean toRun = false;
 private Test Test;
 private SurfaceHolder surfaceHolder;

 public UT(Test rTest) {
 Test = rTest;
 surfaceHolder = Test.getHolder();
 }

 public void setRunning(boolean run) {
 toRun = run;
 }

 @SuppressLint("WrongCall")
@Override
 public void run() {
 Canvas c;
 while (toRun) {

 long cTime = System.currentTimeMillis();

 if ((cTime - time) <= (1000 / fps)) {

 c = null;
 try {
 c = surfaceHolder.lockCanvas(null);
 Test.onDraw(c);
 } finally {

 if (c != null) {
 surfaceHolder.unlockCanvasAndPost(c);
 }
 }
 }
 time = cTime;
 }
 }
}

0 个答案:

没有答案