我有启动画面问题。它仅显示半秒钟,然后在活动HomeZastepowy出现之前消失几秒钟。我根据此站点https://android.jlelse.eu/right-way-to-create-splash-screen-on-android-e7f1709ba154制作了此初始屏幕。我在哪里弄错了?
MainActivity代码:
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null) {
String uid = mAuth.getUid();
final DocumentReference mDocRef = FirebaseFirestore.getInstance().collection("Data").document("userInfo").collection(Objects.requireNonNull(uid)).document(uid);
mDocRef.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
String user = documentSnapshot.getString("user");
assert user != null;
if (user.equals("user")) {
Log.d("tag", String.valueOf(mAuth.getUid()));
Intent intent = new Intent(getApplicationContext(),
HomeUsrActivity.class);
startActivity(intent);
finish();
} else {
Log.d("tag", "Zastepowy");
Log.d("tag", String.valueOf(mAuth.getUid()));
startActivity(new Intent(MainActivity.this, HomeZastepowy.class));
finish();
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
finish();
}else {
Intent intent = new Intent(getApplicationContext(),
HomeActivity.class);
startActivity(intent);
finish();
}
}
}
AndroidManifest代码:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zapp4">
<application
android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".HomeUsrActivity"></activity>
<activity android:name=".LoginActivity" />
<activity android:name=".punktyog" />
<activity android:name=".punktywz" />
<activity android:name=".HomeZastepowy" />
<activity android:name=".tradycje" />
<activity android:name=".wiedza" />
<activity android:name=".kontakt" />
<activity android:name=".zastepy" />
<activity android:name=".historia" />
<activity android:name=".onas" />
<activity android:name=".HomeActivity" />
<activity android:name=".MainActivity" android:theme="@style/SplashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最佳答案
我认为问题是您忘记从代码中删除此行:
setContentView(R.layout.activity_main)
因为您设置了ContentView,所以它会覆盖您的主题背景。因此,请尝试对此行发表评论并尝试一下。