English 中文(简体)
不能在Pause/onResume之后提取
原标题:Can not draw after onPause/onResume
  • 时间:2012-01-11 15:01:21
  •  标签:
  • android

I am writing my first android game. It is working fine in almost every respect, but when the screen timeout kicks in and I press the button to get back to the game, I find the image of the game state frozen. It appears the main thread is still alive - I can tell this because I have a set of sounds that occur when I drag certain objects around the screen, and the sounds still behave as if everything was working (except that I see nothing other than the frozen screen). If I then select a menu to show the "high scores" activity and then exit back to the game, I find all is well and can continue with all the graphics working properly.

我不敢肯定,为了诊断这一问题,需要列入我法典的哪些部分,但这里是我的想法——如果要求,我会增加:

http://www.un.org。 根据Brigham的建议,我对法典做了两.,但症状相同。

http://www.un.org。 我已经注意到,如果我点一个纽子来煽动“高分点”活动,那么就会有人叫.。 但是,当场外cks(www.em>not)时。

http://www.un.org。 在没有解决办法的情况下,对该问题有很多看法——我认为问题必须十分trick弄——或者也许没有在《规则》中披露。 关于我如何能够diagnose这一问题的一些建议非常有用。

http://www.un.org。 我现在将整个(简化)项目简单地放在桌面上,并将其放在网上:。 - 关键代码都简单明了。 java

    MicksPanelThing mpt = null;
    public MicksThreadThing micks_thread_thing = null;

    @Override
    protected void onPause() // pair with onResume
    {
        super.onPause();
        allow_draw.set(false);
        micks_thread_thing.setRunning(false);
        save_state();
    }


    @Override
    protected void onResume() // pair with onPause
    {
        super.onResume();
        get_state();
        mpt.get_a_new_thread_all_over_again();
        micks_thread_thing.setRunning(true);
        allow_draw.set(true);
    }

////////////////////////////////////////////////////////////////////////////

    public class MicksPanelThing extends SurfaceView implements SurfaceHolder.Callback
    {
        public MicksPanelThing(Context context) 
        {
            super(context);
        }

        public void get_a_new_thread_all_over_again()
        {
            getHolder().addCallback(this);
            micks_thread_thing = new MicksThreadThing(getHolder(), this);
            setFocusable(true);        
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) 
        {
            synchronized (micks_thread_thing.getSurfaceHolder()) 
            {
                if (event.getAction() == MotionEvent.ACTION_DOWN) 
                {
                    // blah
                }
                if (event.getAction() == MotionEvent.ACTION_MOVE && selected_node != -1) 
                {
                    // blah
                }
                if (event.getAction() == MotionEvent.ACTION_UP) 
                {
                     // blah
                }
                return true;
            }
        }

        @Override
        public void onDraw(Canvas canvas) 
        {
            canvas_width = canvas.getWidth();
            canvas_height = canvas.getHeight();

            if (allow_draw.get())
            {   
                canvas.drawBitmap(sandy_bitmap, 0, 0, null);                

                // blah
            }

        }

        public void surfaceCreated(SurfaceHolder holder) 
        {
            micks_thread_thing.setRunning(true);
            micks_thread_thing.start();
            we_have_a_surface = true;
        }

        public void surfaceDestroyed(SurfaceHolder holder) 
        {
            boolean retry = true;
            micks_thread_thing.setRunning(false);
            while (retry) 
            {
                try 
                {
                    micks_thread_thing.join();
                    retry = false;
                    micks_thread_thing = null;
                } 
                catch (InterruptedException e) 
                {
                    // we will try it again and again...
                }
            }
            we_have_a_surface = false;
        }
    }

    class MicksThreadThing extends Thread 
    {
        private SurfaceHolder surf_holder = null;
        private MicksPanelThing micks_panel_thing;
        private boolean this_thread_is_currently_active = false;

        public MicksThreadThing(SurfaceHolder surfaceHolder, MicksPanelThing mpt) 
        {
            surf_holder = surfaceHolder;
            micks_panel_thing = mpt;
        }

        public void setRunning(boolean set_run) 
        {
            this_thread_is_currently_active = set_run;
        }

        public SurfaceHolder getSurfaceHolder() 
        {
            return surf_holder;
        }

        @Override
        public void run() 
        {
            Canvas c;
            while (this_thread_is_currently_active) 
            {
                c = null;
                try 
                {
                    c = surf_holder.lockCanvas(null);
                    synchronized (surf_holder) 
                    {
                        micks_panel_thing.onDraw(c);
                    }
                } 
                finally 
                {
                    if (c != null) 
                    {
                        surf_holder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }

public class Allow_draw
{
    private boolean draw;

    Allow_draw()
    {
        draw = false;
    }
    public void set(boolean x)
    {
        synchronized (this)
        {
            draw = x;
        }
    }
    boolean get()
    {
        synchronized (this)
        {
            return draw;
        }
    }
}
问题回答

你的问题是,你保留了<条码>surf_holder/code>,因为你的read子成员错了。 当你的活动到睡觉时,然后回到地表持有人时,情况也在发生变化,也可能出现在屏幕上或其它地表上。 因此,最好的做法是,在你们每次都征求意见。

@Override
        public void run() 
        {
            Canvas c;
            while (this_thread_is_currently_active) 
            {
                c = null;
                try 
                {
                    Final SurfaceHolder holder = getHolder();
                    c = holder.lockCanvas(null);
                    if(c != null)// may happen when SurfaceHolder is changing
                        synchronized (surf_holder) 
                        {
                            micks_panel_thing.onDraw(c);
                        }
                    }
                } 
                finally 
                {
                    if (c != null) 
                    {
                        surf_holder.unlockCanvasAndPost(c);
                    }
                }
            }
        }

无。 你这样做是错误的!

移除你所有的指向物,不将其藏匿在任何地方,排除你利用这一功能来接手,将其从你的read子中删除! 只有当你即将锁定血管时:

//inside surfaceView
    public void updateView(){
        final SurfaceHolder holder = getHolder();

        try{
        Canvas canvas = holder.lockCanvas();
        if(canvas != null){
            onDraw(canvas);
        }
        holder.unlockCanvasAndPost(canvas);
        }
        catch (Exception e) {
            // TODO: handle exception
        }
    }

页: 1 例在您打电话setRunning(false)后死亡。 因为while loop conditions to false, and the run <>/code>methwals.

You should either create a new MicksThreadThing in onResume or use some sort of synchronization to pause the run method until the user resumes the game. I recommend the CountDownLatch class to accomplish this. It would allow your run method to call countDownLatch.await(), at which point the thread would pause until you call countDownLatch.countDown() in the onResume method.

此外,<代码>该编码_thread_is_ 当前_active 可变:<条码>

private volatile boolean this_thread_is_currently_active = false;

Try this: In your Surface Destroyed method add this:

holder.unlockCanvasAndPost(MainThread.canvas);

EDIT:

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d("SURFACEDESTROYED","TRUE");
    boolean retry = true;
    while (retry) {
        try {
            holder.unlockCanvasAndPost(MainThread.canvas);
            thread.setRunning(false);
            thread.join();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签