我对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)主题。 –