我對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();
}
};
}}
我覺得你有足夠的答案,但問題的主要原因是,你在不同的線程中打開活動。您應該在Android主線程中運行,您可以閱讀[this](https://developer.android.com/guide/components/processes-and-threads.html)主題。 –