1

同じアクティビティ内でOpenGLViewまたはAndroidGameViewを破棄して再起動しようとしていますが、同じアクティビティ内で破棄した後、ゲームをもう一度開始できないようです。これがmonodroidゲームサンプルプロジェクトを使用した私のテストです:

GLView1 view;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Create our OpenGL view, and display it
        //view = new GLView1(this);
        //SetContentView(view);

        Timer timer = new Timer(OnTimerDone, this, 3000, 3000);
    }

    void OnTimerDone(object state)
    {
        System.Diagnostics.Debug.WriteLine("timer");
        ((Activity)state).RunOnUiThread(() =>
            {
                if (view != null)
                {
                    //view.Stop();
                    view.Dispose();
                    view = null;
                    SetContentView(null);
                    GC.Collect();
                }
                else
                {
                    view = new GLView1((Activity)state);
                    //view.Resume();
                    SetContentView(view);
                }
            });
    }

    //protected override void OnPause()
    //{
    //    base.OnPause();
    //    view.Pause();
    //}

    //protected override void OnResume()
    //{
    //    base.OnResume();
    //    view.Resume();
    //}

よろしくお願いします。

SetContentViewの再利用を避けるために、新しいコードで更新します。

GLView1 view;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Create our OpenGL view, and display it
        //view = new GLView1(this);
        //SetContentView(view);
        SetContentView(Resource.Layout.Main);
        Timer timer = new Timer(OnTimerDone, this, 3000, 3000);
    }

    void OnTimerDone(object state)
    {
        ((Activity)state).RunOnUiThread(() =>
            {
                LinearLayout linearLayoutMain = ((Activity)state).FindViewById<LinearLayout>(Resource.Id.linearLayoutMain);
                if (view != null)
                {
                    System.Diagnostics.Debug.WriteLine("timer delete");
                    linearLayoutMain.RemoveView(view);
                    try
                    {
                        view.Stop();
                        view.Dispose();
                        view = null;
                        //SetContentView(null);
                        GC.Collect();
                    }
                    catch (Exception ex)
                    {
                        //Android.Util.Log.Debug("ex:", ex.ToString());
                        System.Diagnostics.Debug.WriteLine("ex:" + ex);
                    }
                }
                else
                {
                    view = new GLView1((Activity)state);
                    view.Run();
                    //view.Resume();
                    //SetContentView(view);
                    linearLayoutMain.AddView(view);
                    System.Diagnostics.Debug.WriteLine("timer create");

                }
            });
    }
4

1 に答える 1

1

私はあなたのコードをいじってみましたが、Timer まだ実行されていましたが、内部のブロックRunOnUiThread()が呼び出されていませんでした。view.Stop()メソッドとを削除すると、view.Dispose()正しく機能し始めました。

これが私の完全なコードです(時間に5000msの間隔を使用)

    GLView1 view;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);
        Timer timer = new Timer(OnTimerDone, this, 5000, 5000);
    }

    void OnTimerDone(object state)
    {
        RunOnUiThread(() =>
        {
            try
            {
                LinearLayout linearLayoutMain = ((Activity)state).FindViewById<LinearLayout>(Resource.Id.linearLayoutMain);

                if (view != null)
                {
                    linearLayoutMain.RemoveView(view);
                    view = null;
                }
                else
                {
                    view = new GLView1(this);
                    view.Run();
                    linearLayoutMain.AddView(view);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        });
    }

古いビューはまだガベージコレクションされているようです(以下のログを参照)。余分な電話は必要なかったと思います。アプリケーションを約15分間、問題なく実行しました。

11-16 00:30:05.828 D/dalvikvm( 2144): GC_EXPLICIT freed 119K, 6% free 8813K/9351K, paused 3ms+5ms, total 56ms
11-16 00:30:08.047 D/dalvikvm( 2144): GC_CONCURRENT freed 992K, 12% free 8270K/9351K, paused 4ms+36ms, total 162ms
11-16 00:30:10.667 D/dalvikvm( 2144): GC_CONCURRENT freed 202K, 10% free 8467K/9351K, paused 5ms+63ms, total 116ms
于 2012-11-16T00:39:41.480 に答える