Android SurfaceHolder(活动崩溃)
我有一个问题,它是:我去活动,然后回到菜单,然后我再次尝试从菜单转到相同的活动。在第二次尝试中,我得到一个错误“内存不足”,Choreographer.class在mCallbacksRunning=true;}之后的第行打开,并显示一个箭头Android SurfaceHolder(活动崩溃),android,Android,我有一个问题,它是:我去活动,然后回到菜单,然后我再次尝试从菜单转到相同的活动。在第二次尝试中,我得到一个错误“内存不足”,Choreographer.class在mCallbacksRunning=true;}之后的第行打开,并显示一个箭头 void doCallbacks(int callbackType, long frameTimeNanos) { CallbackRecord callbacks; synchronized (mLock) {
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) {
}
}
}
}
并更新胎面花纹:
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;
}
}
}
公共类UT扩展线程{
私人时间长;
私人最终整数fps=20;
私有布尔函数toRun=false;
私人测试;
私人地勤人员地勤人员;
公共UT(测试rTest){
试验=rTest;
surfaceHolder=Test.getHolder();
}
公共void setRunning(布尔运行){
toRun=运行;
}
@SuppressLint(“错误呼叫”)
@凌驾
公开募捐{
帆布c;
while(托伦){
长cTime=System.currentTimeMillis();
如果((cTime-时间)请重新格式化您的代码。不确定确切的问题是什么,但请注意,您的更新线程将为您带来一些问题,可能与您看到的有关。首先,它基本上处于一个没有睡眠或阻塞的紧密循环中。这将导致CPU持续使用,并不必要地耗尽电池。s其次,由于循环很紧,存在一个争用条件,即调用surfaceDestroyed()
回调并在线程中设置标志,但线程可能已经尝试锁定不再存在的曲面。好的,我将寻找更新线程的替代方法。