2017-08-16 62 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

請與我分享你的崩潰日誌。或讓我知道發生了什麼異常。 –