从另一个活动返回时,SurfaceView崩溃

时间:2011-11-07 14:06:11

标签: android multithreading layout surfaceview crash

我的SurfaceView有一个线程问题,它很常见,但我没有在任何地方找到解决方案。

我在框架布局中使用SurfaceView作为背景:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/blobmusic_layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.myapp.Background
    android:id="@+id/background_blobmusic"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
    ...

我的后台类是tipical surfaceview类,持有者,将调用我们的onDraw方法的线程等等。一些代码可以避免问题(在构造函数方法中调用init()):

    public void init() {
    ready = false;
    holder = getHolder();
    holder.addCallback( new Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            boolean retry = true;
            while (retry) {
                try {
                    bgdt.setRunning(false);
                    bgdt.join();
                    retry = false;
                } catch (InterruptedException e) {}
            }
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            holder.setFormat(PixelFormat.RGBA_8888);
            estrelles = new Estrelles(getResources(),getHeight());
            bgdt.setRunning(true);
            bgdt.start();
            ready = true;
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // TODO Auto-generated method stub

        }
    });

和我的线程类(bgtd):

public class BGDrawThread extends Thread {
private Background view;
static final long FPS = 60;
private boolean running = false;

public BGDrawThread(Background view) {
      this.view = view;
}

public boolean isRunning() {
    return running;
}

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

@Override
public void run() {
    long ticksPS = 1000/FPS;
    long startTime;
    long sleepTime;
    //fps checker
    long contms=0;
    long lasttimecheck = System.currentTimeMillis();
    int fps=0;
    while (running) {
        if(contms>1000) {
            //souts ??
            contms=0;
            fps=0;
            lasttimecheck = System.currentTimeMillis();
        }
        else {
            fps++;
            contms+=System.currentTimeMillis()-lasttimecheck;
            lasttimecheck=System.currentTimeMillis();
        }
        Canvas c = null;
        startTime = System.currentTimeMillis();
        try {
            c = view.getHolder().lockCanvas();
            synchronized (view.getHolder()) {
                view.onDraw(c);
            }
        } finally {
            if (c != null) {
                view.getHolder().unlockCanvasAndPost(c);
            }
        }
        sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
        try {
            if (sleepTime > 0)
                   sleep(sleepTime);
            else
                   sleep(10);
     } catch (Exception e) {}
    }
}
}

这很好用,直到另一个活动出现在前面,然后我们从它回来,崩溃。

这是我的类的onpause / onresume方法的代码:

    @Override
protected void onResume() {
    super.onResume();
    Background bmbg = (Background) findViewById(R.id.background_blobmusic);
    bmbg.resumethread();
}
@Override
protected void onPause() {
    super.onPause();
    Background bmbg = (Background) findViewById(R.id.background_blobmusic);
    bmbg.stopthread();
}

和surfaceview类中的strop / resume线程:

    public void stopthread() {
    if(!bgdt.isAlive()) return;
    boolean retry = true;
    while (retry) {
        try {
            bgdt.setRunning(false);
            bgdt.join();
            retry = false;
        } catch (InterruptedException e) {}
    }
}
public void resumethread() {
    if(ready && !bgdt.isRunning()) {
        bgdt.setRunning(true);
        bgdt.start();
    }
}

欢迎所有帮助:)

编辑:我让它运行(可能不是一个优雅的解决方案,但我在这里留下代码): (查看下面的评论以查看其他更改)

public void startthread() {
    if(!surfacecreated) return;
    if(threadrunning) return;
    bgdt = new BGDrawThread(this);
    bgdt.setRunning(true);
    bgdt.start();
    threadrunning = true;
}

public void stopthread() {
    boolean retry = true;
    while (retry) {
        try {
            bgdt.setRunning(false);
            bgdt.join();
            threadrunning = false;
            retry = false;

        } catch (InterruptedException e) {}
    }
}

1 个答案:

答案 0 :(得分:0)

IIRC表面只有在执行了surfaceChanged()回调之后才能进行操作,而不是在surfaceCreated()之后,并且在onResume()之后调用surfaceChanged() - 将初始化和线程启动移动到那里(到surfaceChanged())