Ich mache einen Startbildschirm für meine App und ich teste nur aus dem primären Thread in den Ruhezustand statt einen Timer verwenden. Mein Code ist:Launcher-Aktivität wird als leerer Bildschirm angezeigt
package com.example.somu.activityswitcher;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class LauncherActivity extends AppCompatActivity {
public void firstActivity() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
TextView countDown = findViewById(R.id.count);
for (int cd=3;cd>0;cd--) {
try {
Thread.sleep(1000);
countDown.setText(Integer.toString(cd));
} catch (Exception e) {
e.printStackTrace();
}
}
firstActivity();
}
}
Während die MainActivity Lasten nach 3 Sekunden, der Begrüßungsbildschirm (LauncherActivity) ist nur ein leerer Bildschirm! Was ist denn hier los?!
activity_launcher.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.somu.activityswitcher.LauncherActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:scaleX="0.25"
android:scaleY="0.25"
app:srcCompat="@drawable/logo" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/count"
android:layout_centerHorizontal="true"
android:text="Switching in..." />
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:text="3"
android:textColor="@android:color/black"
android:textSize="50sp" />
</android.widget.RelativeLayout>
Wie kann ich dieses Problem beheben ?!
HINWEIS: Ich bin nicht über jede mögliche Weise störte dies zu beheben .. Ich möchte wissen, warum genau diese Methode nicht funktioniert, und was ist die nächste beste Art und Weise, ohne explizit einen Timer.
ersetzen, dass lahm beschäftigt Schleife mit 'postDelayed()' und 'richtigen Runnable' –
@MarcinOrlowski natürlich das würde funktionieren. Lies meine ursprüngliche Frage, falls du es nicht schon getan hast.Ich wollte keinen Timer benutzen und wollte nur wissen, warum das nicht funktionieren würde. Trotzdem danke. :) –
@MarcinOrlowski oh, und der einzige Grund, warum ich diese Schleife verwendet habe, war, den Countdown auf dem Bildschirm zu zeigen. –