簡體   English   中英

啟動畫面固定在屏幕上-主要活動意圖未開始

[英]Splash screen stuck to the screen - intent to the main activity is not starting

我正在使用Thread.sleep()在android中創建啟動畫面。 (我知道另一種方法-使用handler ,但是我現在必須使用此方法。)

我的代碼如下:

public class SplashScreen extends Activity { 
   Thread t;
             @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_spash_screen);

            new myclass();
        }
            class myclass implements Runnable{

                myclass()
                {
                t = new Thread();
                t.start();
                }

                public void run()
                {
                    try{
                        Thread.sleep(1000);
                    Intent i = new Intent(getApplicationContext(), MainActivity.class);
                       startActivity(i);
                       finish();
                    }
                    catch(InterruptedException e){
                    System.out.println("thread interrupted");

                    }
                    }

                }
            }

它沒有顯示任何錯誤,但初始屏幕卡在了屏幕上。

1s之后,它沒有開始另一個intent

如果您知道錯誤,請幫助我。

不調用runnable的run方法,因為您沒有將runnable傳遞給Thread構造函數。 因此將其傳遞為:

            t = new Thread(this);

試試這個,我總是在我的飛濺活動中使用此代碼。

 public class SplashScreen extends Activity
   {
private Thread mSplashThread;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);

    mSplashThread = new Thread()
    {
        @Override
        public void run()
        {
            try
            {
                synchronized (this)
                {
                    wait(2000);
                }
            }
            catch(InterruptedException ex) {
                ex.printStackTrace();
            }
            finish();
            startActivity(new Intent(getApplicationContext(),MainActivity.class));
        }
    };
    mSplashThread.start();
}

@Override
public boolean onTouchEvent(MotionEvent evt)
{
    if (evt.getAction() == MotionEvent.ACTION_DOWN)
    {
        synchronized (mSplashThread)
        {
            mSplashThread.notifyAll();
        }
    }
    return true;
}
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM