【问题标题】:Unable to redirect after Splash screen启动画面后无法重定向
【发布时间】:2013-10-17 10:47:40
【问题描述】:

我已经设置了一个启动屏幕,我希望它在 5 秒后重定向到我的主要活动,但我的应用程序始终停留在启动屏幕上并且从不重定向。

我的SplashActivity.Java中有以下代码

public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.SplashActivity);
        Thread timer = new Thread() {
            public void run() {
                try {
                    // sleep(R.integer.SplashActivityTime);
                    sleep(5000);
                } catch (InterruptedException iEx) {
                    iEx.printStackTrace();
                } finally {
                    Intent mainActivity = new Intent(
                           "com.myApp.myApp.MainActivity");

                    startActivity(mainActivity);
                }
            }
        };
        timer.start();
    }
}

Manifest 我有:

<activity
    android:name="com.myApp.myApp.SplashActivity"
    android:label="@string/app_name" >

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name="com.myApp.myApp.MainActivity"
    android:label="@string/app_name" >

    <intent-filter>
        <action android:name="com.myApp.myApp.MainActivity" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

【问题讨论】:

  • 您是否尝试过在 Intent 中使用 MainActivity.class?
  • startActivity(new Intent(getApplicationContext(), com.myApp.myApp.MainActivity.class));
  • 在下面的链接中查看我的答案,它可能会对你有所帮助.....stackoverflow.com/questions/18762484/…

标签: java android multithreading


【解决方案1】:

使用此代码

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

    Thread timer = new Thread() {
        public void run() {
            try {
                // sleep(R.integer.SplashActivityTime);
                sleep(5000);
            } catch (InterruptedException iEx) {
                iEx.printStackTrace();
            } finally {
                Intent mainActivity = new Intent(MainActivity.this,
                        SecondActivity.class);

                startActivity(mainActivity);
                finish();
            }
        }
    };
    timer.start();
}

在清单中

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.sample.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

     <activity
        android:name="com.example.sample.SecondActivity"
        android:label="@string/app_name" >

    </activity>
</application>

【讨论】:

    【解决方案2】:

    改变你的意图:

    Intent mainActivity = new Intent(this, MainActivity.class);
    

    另外,哪个类是那个sleep方法的所有者?

    【讨论】:

      【解决方案3】:

      删除清单文件中主要活动活动的意图过滤器

      实现闪屏的最佳方式click here

      并且起始意图线更改为

      Intent mainActivity = new Intent(SplashActivity .this, MainActivity.class);
      

      【讨论】:

        【解决方案4】:

        使用以下代码

        Thread logoTimer = new Thread()
            {
                @Override
                public void run()
                {
                    try
                    {
                        sleep(3000);
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    }//End of try block
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }//End of catch block
                    finally
                    {
                        finish();
                    }//End of finally block
                }//End of run method
            };//End of anonymous thread inner class
            logoTimer.start();
        

        Manifest

        <activity
            android:name="com.myApp.myApp.SplashActivity"
            android:label="@string/app_name" >
        
            <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
        </activity>
        <activity
                android:name="com.myApp.myApp.MainActivity"
                android:label="@string/app_name" >
            </activity>
        

        【讨论】:

          【解决方案5】:

          也许试试这个:

          公共类闪屏扩展了 Activity {

              public void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
          
                  setContentView(R.layout.splash); // Your Splash Layout
          
                  Handler handler = new Handler();
          
               // run a thread
                  handler.postDelayed(new Runnable() {
          
                      public void run() {
          
                          // make sure we close the splash screen so the user won't come back when it presses back key
          
                          finish();
                          // start the home screen
          
                          Intent intent = new Intent(splashscreen.this, TabDemo2Activity.class);
                          splashscreen.this.startActivity(intent);
          
                      }
          
                  }, 5000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
          
          }
          }
          

          【讨论】:

            【解决方案6】:

            使用下面的处理程序对我有用。

            Handler handler = new Handler();

            // run a thread after 2 seconds to start the home screen
            
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                            if (!mIsBackButtonPressed) {
                                // start next activity
                                Intent intent = new Intent(SplashScreen.this, SEOshopMainActivity.class);
                                startActivity(intent);
                           } 
                        }
                    }, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2013-06-14
              • 1970-01-01
              • 2013-06-16
              • 2021-04-23
              • 1970-01-01
              • 2021-05-10
              • 1970-01-01
              相关资源
              最近更新 更多