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

在实例化线程时,您没有将 runnable 作为参数传递给 Thread 构造函数。当您在 Splash.class 中实现接口 Runnable 时,将当前对象作为参数传递。

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

希望这有帮助。

于 2013-11-09T20:54:29.740 回答
1

Instaed 使用此代码启动您的活动。

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

you can try this simple code..

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    *// activity_splay is the name of splash .xml file*
     setContentView(R.layout.activity_splash);
    Thread thread = new Thread(){
        @Override
        public void run(){
            try{
                // splash screen would disappear after 1500 ms i.e 1.5 sec
                sleep(1500); <br/>
         // MainActivity is the name of activity that would appear after splash screen 
         startActivity(new Intent(getApplicationContext(), MainActivity.class));
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    };
    thread.start();
}
//hopefully it would help:)
于 2015-02-17T18:42:41.707 回答
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 回答