Android Studio Splash错误(它在介绍屏幕上冻结)

z9smfwbn  于 2023-11-21  发布在  Android
关注(0)|答案(1)|浏览(208)

我正在通过Splash制作一个介绍屏幕。我必须显示介绍屏幕3秒钟,然后转到登录屏幕,但它仍然冻结在介绍屏幕上。
问题出在哪里?对应的源代码:
SlashActivity.java(splash class)
public class Jumper {

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

    try {
        Thread.sleep(3000);
    } catch (Exception e) {

        Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
        startActivity(intent);
        finish();
    }
}

字符串
}
LoginActivity.java
public class LoginActivity extends AppCompatActivity {

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

    mFirebaseAuth = FirebaseAuth.getInstance();
    mDatabaseRef = FirebaseDatabase.getInstance().getReference();

    EtEmail = findViewById(R.id.EtEmail);
    EtPwd = findViewById(R.id.EtPwd);
    Btn_Register = findViewById(R.id.Btn_Register);
    Btn_Login = findViewById(R.id.Btn_Login);

    Btn_Login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(EtEmail.getText().length() > 0 && EtPwd.getText().length() > 0) {
                String strEmail = EtEmail.getText().toString();
                String strPwd = EtPwd.getText().toString();
                mFirebaseAuth.signInWithEmailAndPassword(strEmail, strPwd).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // 로그인 성공
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast.makeText(LoginActivity.this, "로그인 실패", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            } else {
                Toast.makeText(LoginActivity.this, "아이디와 비밀번호를 확인해주세요.", Toast.LENGTH_SHORT).show();
            }
        }
    });
    Btn_Register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        }
    });
        }
    }


AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<application
    android:usesCleartextTraffic="true"
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_icon"
    android:label="HomePet"
    android:roundIcon="@mipmap/ic_icon"
    android:supportsRtl="true"
    android:theme="@style/Theme.Project"
    tools:targetApi="31" >

    <activity android:name=".SlashActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".map.MapsActivity"
        android:exported="false"
        android:label="MapsActivity" />
    <activity
        android:name=".User_Account"
        android:exported="false" />
    <activity
        android:name=".RegisterActivity"
        android:exported="false" />
    <activity
        android:name=".LoginActivity"
        android:exported="true" >
    </activity>
    <activity
        android:name=".MainActivity"
        android:exported="true" >
    </activity>

    <activity android:name=".Useredit_Activity"/>
    <activity android:name=".LogoutActivity"/>
    <activity android:name=".NoticeActivity"/>
    <activity android:name=".HospitalActivity"
        android:exported="false"/>
</application>


我真的很感激你的帮助。
为什么会出错?


的数据

ecr0jaav

ecr0jaav1#

try中,您冻结了主线程3秒。不建议这样做。它不会进入catch,因为它成功工作。您可以使用此handler代替。

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

    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }, 3000);
}

字符串

相关问题