2017-08-16 56 views
-2

我对Android很新,我正在开发一个应用程序。我想运行启动画面,但在我的代码中出现错误,启动画面只是打开并停留在同一页面,但它不会打开下一个活动,请任何人帮助我。我的主要目标是“对于新用户,它应该首先打开启动屏幕Setpassword.java活动(并且密码应该存储在sharedpreference),但是当用户设置密码并关闭应用程序并且当他重新打开时,应该打开。直接enterpassword.java活动,即跳过启动画面setpassword Java的活动:splashscreen下一个活动不开放

package com.example.shiva.secretbook; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

import java.util.logging.Handler; 

/** 
* Created by shiva on 8/12/2017. 
*/ 
public class SplashScreenActivity extends Activity { 
String password; 
ImageView imageViewSplash; 
TextView txtAppName; 
RelativeLayout relativeLayout; 
Thread SplashThread; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_splash_screen); 
    // load password 
    SharedPreferences settings = getSharedPreferences("PREFS", 0); 
    password = settings.getString("password", ""); 
    imageViewSplash = (ImageView) findViewById(R.id.imageViewSplash); 
    txtAppName = (TextView) findViewById(R.id.txtAppName); 
    relativeLayout = (RelativeLayout) findViewById(R.id.relative); 
    startAnimations(); 
} 

private void startAnimations(){ 
    Animation rotate = AnimationUtils.loadAnimation(this,R.anim.rotate); 
    Animation translate = AnimationUtils.loadAnimation(this, 
R.anim.translate); 

    rotate.reset(); 
    translate.reset(); 
    relativeLayout.clearAnimation(); 

    imageViewSplash.startAnimation(rotate); 
    txtAppName.startAnimation(translate); 
    SplashThread = new Thread(){ 
     @Override 
     public void run() { 
      super.run(); 
      int waited = 0; 
      while (waited < 3500) { 
       try { 
        sleep(100); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       waited += 100; 

      } 
      if (password.equals("")){ 
       // if there is no password 
       SplashScreenActivity.this.finish(); 
       Intent intent = new 
    Intent(SplashScreenActivity.this,setpassword.class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
       startActivity(intent); 
       finish(); 
      } else { 

       //if there is a password 
       Intent intent = new 
    Intent(SplashScreenActivity.this,enterpassword.class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
       startActivity(intent); 
       finish(); 
    } SplashThread.start(); 
    } 


}; 
}} 
+0

我觉得你有足够的答案,但问题的主要原因是,你在不同的线程中打开活动。您应该在Android主线程中运行,您可以阅读[this](https://developer.android.com/guide/components/processes-and-threads.html)主题。 –

回答

1

试试这个

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(new SplashEnvironment(this, this)); 
    new Handler().postDelayed(new Runnable() { 

    @Override 
    public void run() { 
     if (password.equals("")){ 
      // if there is no password 
      SplashScreenActivity.this.finish(); 
      Intent intent = new 
      Intent(SplashScreenActivity.this,setpassword.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
      startActivity(intent); 
      finish(); 
     } else { 

      //if there is a password 
      Intent intent = new 
Intent(SplashScreenActivity.this,enterpassword.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
      startActivity(intent); 
      finish(); 
} 
     // close this activity 
    } 
    }, 3000);// time for spalsh screen 
+0

兄弟,谢谢你的帮助,但我需要相同的启动画面来运行,因为我们的团队喜欢那个闪屏 –

+0

请引导我在我的代码中,我应该在哪里更改或更新代码 –

+0

检查更新ans @ShivaLucky –

1
 new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        if (password.equals("")) 
       { 
       // if there is no password 
       Intent intent = new Intent(SplashScreenActivity.this,setpassword.class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
       startActivity(intent); 
       finish(); 
       } else 
       { 
       //if there is a password 
       Intent intent = new Intent(SplashScreenActivity.this,enterpassword.class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
       startActivity(intent); 
       finish(); 
       } 
       } 
      }, 3500); 
+0

兄弟,我是非常基本的android,所以请指导我在哪里我应该完全放置该代码,并在公共无效运行功能()我有一些额外的代码,我应该删除它? (一旦检查我的代码兄弟...谢谢) –

1

尝试下面的代码

public void StartAnimation(){ 
     new Handler().postDelayed(new Runnable() { 

      @Override 
      public void run() { 

       if (password.equals("")){ 
        // if there is no password 
        SplashScreenActivity.this.finish(); 
        Intent intent = new 
          Intent(SplashScreenActivity.this,setpassword.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
        startActivity(intent); 
        finish(); 
       } else { 

        //if there is a password 
        Intent intent = new 
          Intent(SplashScreenActivity.this,enterpassword.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
        startActivity(intent); 
        finish(); 
       } 
      } 
     }, 5000); 
    } 
0

试试下面的代码


    package com.wikitude.samples; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.content.SharedPreferences; 
    import android.os.Bundle; 
    import android.os.Handler; 
    import android.view.animation.Animation; 
    import android.view.animation.AnimationUtils; 
    import android.widget.ImageView; 
    import android.widget.RelativeLayout; 
    import android.widget.TextView; 


    /** 
    * Created by shiva on 8/12/2017. 
    */ 
    public class SplashScreenActivity extends Activity { 
     String password; 
     ImageView imageViewSplash; 
     TextView txtAppName; 
     RelativeLayout relativeLayout; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_splash_screen); 
      // load password 
      SharedPreferences settings = getSharedPreferences("PREFS", 0); 
      password = settings.getString("password", ""); 
      imageViewSplash = (ImageView) findViewById(R.id.imageViewSplash); 
      txtAppName = (TextView) findViewById(R.id.txtAppName); 
      relativeLayout = (RelativeLayout) findViewById(R.id.relative); 
      startAnimations(); 
     } 

     private void startAnimations() { 
      Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate); 
      Animation translate = AnimationUtils.loadAnimation(this,R.anim.translate); 

      rotate.reset(); 
      translate.reset(); 
      relativeLayout.clearAnimation(); 
      imageViewSplash.startAnimation(rotate); 
      txtAppName.startAnimation(translate); 

      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        runOnUiThread(launchNextScreen); 
       } 
      }, 3500); 

     } 

     public Runnable launchNextScreen = new Runnable() { 
      @Override 
      public void run() { 
       if (password.equals("")) { 
        // if there is no password 
        SplashScreenActivity.this.finish(); 
        Intent intent = new Intent(SplashScreenActivity.this, setpassword.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
        startActivity(intent); 
        finish(); 
       } else { 

        //if there is a password 
        Intent intent = new Intent(SplashScreenActivity.this, enterpassword.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
        startActivity(intent); 
        finish(); 
       } 
      } 
     }; 
    } 
+0

兄弟,非常感谢,但它显示错误在runOnUiThread(launchNextScreen),(无法找到符号launchNextScreen),也非法启动私有表达式Runnable launchNextScreen = new Runnable() –

+0

@ShivaLucky:确保你已经导入'import android.os.Handler;'在您的活动中, –

+0

是兄弟,仍然显示“找不到符号launchNextScreen” –

0

你开始你的线程(SplashThread.start();)创建线程内。

由于此,您的线程无法启动。因此,只需将此线放在正确的位置即可。

SplashThread = new Thread(){

}; SplashThread.start();

+0

欢迎来到SO,已经给出了一些正确的答案。请通过阅读部分来了解如何在SO上提问和回答问题。干杯。 –

+0

是兄弟,tq,但闪屏后它显示,不幸的是它停止工作 –

+0

请与我分享你的崩溃日志。或让我知道发生了什么异常。 –