简体   繁体   English

应用程序打开时黑屏

[英]Black Screen When App Opens

I keep getting this black screen when ever I open my app, the only way to get rid of it is to press the back button, then again go to the app.当我打开我的应用程序时,我总是会出现这个黑屏,摆脱它的唯一方法是按下后退按钮,然后再次按下 go 到应用程序。 I was told by my friend that it is going into deadlock.我的朋友告诉我,它正在陷入僵局。 but I do not think because when I checked the logcat it shows the below log messages.但我不认为,因为当我检查 logcat 时,它会显示以下日志消息。

View看法

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"); 
}

Thread线

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();
        }
    }

Log日志

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

Activity Class:活动 Class:

@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");   

}

Does rotating the display cause it to refresh (in emulator: CTRL+F12)?旋转显示器是否会导致它刷新(在模拟器中:CTRL+F12)? If so then you have to override onResume() in your activity and have it restart whatever it is you set up that might not be being called.如果是这样,那么您必须在您的活动中覆盖 onResume() 并让它重新启动您设置的任何可能不会被调用的内容。 It could be that you just need to call view.invalidate() from the activity onResume()?可能您只需要从活动 onResume() 中调用view.invalidate() )? You don't show code from your activity so I'm unsure.您没有显示活动中的代码,所以我不确定。

In the code I saw, you should use this.postInvalidate() (same as invalidate() but for from a different thread) instead of this.onDraw(canvas) and have it call this.onDraw() on its own in the UI thread, as opposed to in your thread.在我看到的代码中,您应该使用this.postInvalidate() (与invalidate()相同,但来自不同的线程)而不是this.onDraw(canvas)并让它在 UI 中自行调用this.onDraw()线程,而不是在您的线程中。

Where do you initialize your thread?你在哪里初始化你的线程? Try doing so in surfaceCreated( ) instead of the surfaceView consturctor (which is where I think you did it..)尝试在surfaceCreated( ) 中而不是surfaceView 构造函数中这样做(我认为你是在这个构造函数中这样做的..)

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();  

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM