应用程序打开时黑屏

时间:2011-07-08 22:28:26

标签: java android

当我打开我的应用程序时,我一直得到这个黑屏,摆脱它的唯一方法是按下后退按钮,然后再次转到应用程序。我的朋友告诉我,它会陷入僵局!但我不认为因为当我检查logcat时它会显示以下日志消息。

查看

private boolean mGameIsRunning;

public void surfaceCreated(SurfaceHolder holder) {
    // Your own start method.
    start();

}

public void start() {
    if (!mGameIsRunning) {
        thread.initLevel();
        thread.setRunning(true);
        thread.start();   

        mGameIsRunning = true;
    } else {

        thread.onResume();

        thread.initLevel();
        thread.setRunning(true);
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d(TAG, "Surface is being destroyed");

    // tell the thread to shut down and wait for it to finish
    // this is a clean shutdown
    boolean retry = true;
    thread.setRunning(false);       
    while (retry) {     
        try {
            thread.join();
            retry = false;  
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           
    }
    Log.d(TAG, "Thread was shut down cleanly"); 
}

private Object mPauseLock = new Object();  
    private boolean mPaused;
    @Override
    public void run() {
        Canvas canvas;
        Log.d(TAG, "Starting game loop");
        while (running) {
            canvas = null;
            // try locking the canvas for exclusive pixel editing
            // in the surface
            try {
                canvas = this.mSurfaceHolder.lockCanvas();
                synchronized (mSurfaceHolder) {
                    mStartTime = System.currentTimeMillis();
                    mElapsedTime = System.currentTimeMillis() - mStartTime;

                    this.updateGame();
                    this.onDraw(canvas);
                }

                synchronized(mPauseLock){
                    while (mPaused) {

                        try {
                            mPauseLock.wait();
                        } catch (InterruptedException e) {
                        }
                    }   

                }

            } finally {
                // in case of an exception the surface is not left in
                // an inconsistent state
                if (canvas != null) {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
            }   // end finally          
        }       
    }
    public void onPause() {
        synchronized (mPauseLock) {
            mPaused = true;         
        }
    }

    public void onResume() {
        synchronized (mPauseLock) {
            mPaused = false;
            mPauseLock.notifyAll();
        }
    }

日志

07-08 17:24:41.735: DEBUG/Hitman(3221): View added
07-08 17:24:41.895: DEBUG/(3221): Enemies Spawed
07-08 17:24:41.934: DEBUG/(3221): Starting game loop
07-08 17:24:42.165: INFO/ActivityManager(66): Displayed activity com.android.hitmanassault/.Hitman: 1384 ms (total 1384 ms)
07-08 17:24:46.164: DEBUG/(3221): Enemies Spawed
07-08 17:24:48.914: INFO/ActivityManager(66): Start proc com.android.settings for broadcast com.android.settings/.widget.SettingsAppWidgetProvider: pid=3228 uid=1000 gids={3002, 3001, 3003}
07-08 17:24:48.914: INFO/ActivityManager(66): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/.Launcher }
07-08 17:24:48.924: DEBUG/PhoneWindow(3221): couldn't save which view has focus because the focused view com.android.hitmanassault.HitmanView@44d99528 has no id.
07-08 17:24:48.954: INFO/WindowManager(66): Setting rotation to 0, animFlags=0
07-08 17:24:49.014: INFO/ActivityManager(66): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=34}
07-08 17:24:49.275: DEBUG/(3221): Surface is being destroyed
07-08 17:24:49.285: DEBUG/(3221): Thread was shut down cleanly
07-08 17:24:49.694: DEBUG/ddm-heap(3228): Got feature list request
07-08 17:24:49.754: WARN/IInputConnectionWrapper(3221): showStatusIcon on inactive InputConnection
07-08 17:24:51.315: DEBUG/dalvikvm(66): GC freed 2325 objects / 114696 bytes in 122ms
07-08 17:24:58.234: INFO/ActivityManager(66): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.hitmanassault/.HitmanTitle }
07-08 17:24:58.284: INFO/WindowManager(66): Setting rotation to 1, animFlags=0
07-08 17:24:58.354: INFO/ActivityManager(66): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=2 layout=34}
07-08 17:24:58.554: DEBUG/(3221): Enemies Spawed
07-08 17:24:58.945: WARN/IInputConnectionWrapper(144): showStatusIcon on inactive InputConnection
07-08 17:25:00.604: DEBUG/dalvikvm(66): GC freed 1403 objects / 71832 bytes in 103ms

活动类:

@Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.game_menu, menu);
        return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item){

    switch(item.getItemId()){
    case R.id.menu_restart:
        startActivity(new Intent(this, Hitman.class));
        return true;
    case R.id.menu_scores:
        startActivity(new Intent(this, HitmanScores.class));
        return true;
    case R.id.menu_help:
        startActivity(new Intent(this, HitmanHelp.class));
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    setContentView(new CanvasSurfaceView(this));
    Log.d(TAG, "View added");   

}

2 个答案:

答案 0 :(得分:1)

旋转显示器是否会使其刷新(在模拟器中:CTRL + F12)?如果是这样,那么你必须在你的活动中覆盖onResume()并让它重新启动你设置的任何可能没有被调用的东西。可能你需要从活动onResume()调用view.invalidate()?您不会显示活动中的代码,所以我不确定。

在我看到的代码中,您应该使用this.postInvalidate()(与invalidate()相同但是来自不同的主题)而不是this.onDraw(canvas),并在其上调用this.onDraw()拥有UI线程,而不是你的线程。

答案 1 :(得分:1)

你在哪里初始化你的线程?尝试在surfaceCreated()中执行此操作,而不是使用surfaceView consturctor(我认为你在这里做了..)

public void surfaceCreated(SurfaceHolder holder) { 

    // Restart draw thread
    Thread.State state = thread.getState();

    if(state == Thread.State.TERMINATED)
        thread = new MyThread(getHolder(), getContext());   
    thread.setRunning(true);
    thread.start();  

}