【问题标题】:How can I use Intent in postDelayed using Android?如何使用 Android 在 postDelayed 中使用 Intent?
【发布时间】:2021-12-28 23:53:19
【问题描述】:

这是用于启动画面。我已按照教程进行操作,但仍然无法正常工作。一直报错。

这是我的代码:

package id.ac.umn.finalproject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends AppCompatActivity {

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

        Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);

        new Handler().postDelayed(startActivity(startApp), 3000);
    }
}

【问题讨论】:

    标签: java android android-studio android-intent postdelayed


    【解决方案1】:

    试试这个:

    int SPLASH_TIME_OUT = 3000;
    /*
     * Showing splash screen with a timer. This will be useful when you
     * want to show case your app logo / company
     */
    new Handler().postDelayed(() -> {
        // This method will be executed once the timer is over
        // Start your app main activity
        Intent i = new Intent(SplashScreenActivity.this, HomeActivity.class);
        startActivity(i);
    
        // close this activity
        finish();
    }, SPLASH_TIME_OUT);
    

    【讨论】:

      【解决方案2】:

      创建启动画面的正确方法如下

      style.xml

       <style name="SplashStyle" parent="Theme.MaterialComponents.DayNight.NoActionBar">
              <item name="android:windowBackground">@drawable/splash</item>
          </style>
      

      Splash.xml

      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      
          <item android:drawable="@color/black" /> // background color
      
          <item
              android:drawable="@drawable/ic_launcher" /> // logo
              android:gravity="center" />
      
      </layer-list>
      

      清单

      <activity
                  android:name=".SplashActivity"
                  android:exported="true"
                  android:noHistory="true"
                  android:theme="@style/SplashStyle">
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
      
                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
      
              </activity>
      

      SplashActivity.java

      public class SplashActivity extends AppCompatActivity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      //        setContentView(R.layout.activity_splash); // No need setContentView
              
              Intent intent = new Intent(SplashActivity.this,MainActivity.class);
              startActivity(intent);
          }
      

      【讨论】:

        【解决方案3】:

        您几乎正确地使用了postDelayed(Runnable, long),但并不完全正确。让我们看看你的 Runnable。

        final Runnable r = new Runnable() {
            public void run() {
                
               Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);
               handler.postDelayed(startActivity(startApp), 3000);
            }
        };
        

        当我们调用r.run(); 时,它要做的第一件事就是告诉你的handler 在3000 milliseconds 之后运行相同的Runnable,然后调用startApp. 这实际上会导致你的@987654327 @ 是 called twice: 一次,一旦处理程序完成等待 3000 milliseconds.

        相反,您应该将 Runnable 更改为:

        final Runnable r = new Runnable() {
            public void run() {
                 Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);
            }
        };
        

        然后这样称呼它:

        handler.postDelayed(r, 3000);
        

        【讨论】:

          【解决方案4】:

          在 Kotlin 中试试这个:

          Handler(Looper.getMainLooper()).postDelayed({
                     Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);
                    startActivity(startApp)
                  }, 3000)
          

          【讨论】:

            猜你喜欢
            • 2017-07-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-06-27
            相关资源
            最近更新 更多