0

アプリの残りの読み込み中に表示されるスプラッシュ画面であるスレッドを作成しようとしていますが、何らかの理由でスプラッシュ アクティビティが 2 秒後に消えません。何故ですか?

これが私の Splash アクティビティ クラスです。

imports ...

public class Splash extends Activity implements Runnable {

    @Override
    protected void onCreate(Bundle tokenArg) {
        super.onCreate(tokenArg);
        setContentView(R.layout.splash);

        Thread splashing = new Thread();
        splashing.start();

    }

    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            startActivity(new Intent(Splash.this, Home.class));
        }
        catch(Exception excpt) {
            AlertDialog alert = new AlertDialog.Builder(this).create();
            alert.setTitle("Error");
            alert.setMessage("App is going to close");
        }
        finally {
            this.finish();
        }
    }
}

これが .Home アクティビティ クラスです。

public class Home extends Activity {

    @Override
    protected void onCreate(Bundle tokenArg) {
        super.onCreate(tokenArg);
        setContentView(R.layout.activity_home);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }
}

どちらにも対応するxmlがあり、すべて問題ありません。(私はそれらを個別にテストしました)

お時間をいただきありがとうございます。

4

5 に答える 5

2

スレッドのインスタンス化中に、ランナブルをパラメーターとして Thread コンストラクターに渡していません。Splash.class でインターフェイス Runnable を実装したので、現在のオブジェクトをパラメーターとして渡します。

Thread splashing = new Thread(this);
splashing.start();

これが役に立ったことを願っています。

于 2013-11-09T20:54:29.740 に答える
1

スレッドの代わりに、このコードを使用してアクティビティを開始します。

new Handler().postDelayed(new Runnable(){
                public void run() {
                    Intent mainIntent = new Intent(Splash.this, Home.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    Splash.this.startActivity(mainIntent);

                    Splash.this.finish();
                }
            }, 2000); 
于 2013-11-09T20:03:45.960 に答える
0

このコードを試してください。非常に多くのユースケースを処理し、ユーザーの戻るボタンやその他の多くのユースケースを適切に処理します.

public class SplashBranding extends Activity {

    private Handler mHandler;
    private static final long TWO_SECOND_IN_MS = 2000; 

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mHandler = new ShowHomeHandler();   
        setContentView(R.layout.splash);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHandler.sendEmptyMessageDelayed(0, TWO_SECOND_IN_MS);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // In case system dialogs appear and this method is called, we shouldn't show Home. 
        if(!isFinishing())
            mHandler.removeMessages(0);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // In case a call is received and this method is called, we shouldn't show Home.
        if(!isFinishing())
            mHandler.removeMessages(0);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // In case user pressed back, we shouldn't show Home.
        mHandler.removeMessages(0);
    }

    /**
     * Shows the Home Activity.
     */
    private void showHome() {
        Intent i = new Intent(this, Home.class);
        startActivity(i);
        finish();
    }

    private class ShowHomeHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
            case 0:
                showHome();
                break;
            default:
                break;
            }
        }
    }

}
于 2013-11-09T20:43:44.130 に答える
0

のクラスに合格していないようですnew Thread();。に変更してみてくださいnew Thread(this);

于 2013-11-09T22:54:18.770 に答える