【问题标题】:splash activity won't proceed to next activity/class - android studio [duplicate]飞溅活动不会进行下一个活动/课程 - android studio [重复]
【发布时间】:2017-09-21 10:11:24
【问题描述】:

我是 android studio 的新手,我正在做一个启动画面,但它不会进行下一个活动,谁能帮助我:

splash.java

`public class splash extends AppCompatActivity{

private TextView tv;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    tv = (TextView) findViewById(R.id.tv);
    iv = (ImageView) findViewById(R.id.iv);

    Animation myan = AnimationUtils.loadAnimation(this, 
R.anim.mytransition);
    tv.startAnimation(myan);
    iv.startAnimation(myan);

    Thread time = new Thread(){

        public void r () {
            try {
                sleep(5000);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
            finally {
                startActivity(new Intent(splash.this, MainActivity.class));
                finish();

            }

        }

    };
    time.start();

    }


}`

我不知道出了什么问题,但它只是停留在启动屏幕上。

【问题讨论】:

  • 显示你的错误日志。
  • 启动画面最好使用CountDownTimer 而不是sleep

标签: java android


【解决方案1】:

我相信你的线程创建本身是错误的。可以使用 runnable 或通过扩展线程类来创建线程

  Thread time = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                sleep(5000);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
            finally {
                startActivity(new Intent(splash.this, MainActivity.class));
                finish();

            }  
        }
    });
    time.start();

有关线程创建的更多信息

https://www.javatpoint.com/creating-thread

【讨论】:

  • 请解释为什么这应该有效?
  • 更新了描述兄弟 :)
  • 感谢您的描述
【解决方案2】:

此代码完美运行。显示您的动画,然后像我在下面所做的那样调用处理程序:

public class SplashScreen extends AppCompatActivity {

// Splash screen timer
private static int SPLASH_TIME_OUT = 2000;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    //showing splash screen for desired time
    new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity
            Intent i = new Intent(SplashScreen.this, Login.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, SPLASH_TIME_OUT);

}}

【讨论】:

  • 我同意这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多