【问题标题】:Android + Splash Screen crashes total on orientation changeAndroid + Splash Screen 在方向更改时总共崩溃
【发布时间】:2014-02-27 22:16:58
【问题描述】:

我的应用中实际上有 3 个活动。

我刚刚创建了一个活动,并使用处理程序将其作为 SPLASH SCREEN。

即,我的初始屏幕显示 3 秒,然后应用程序的主要生命周期继续。到此为止,一切都很完美。

我的问题是在加载启动画面时,如果我改变方向,整个应用程序都会崩溃。

我的要求是以横向和纵向模式加载应用程序。

我尝试过 onConfig 更改等,但徒劳无功....

我的悲伤故事都包含在这里......

    public class Asplash extends Activity{
Handler handler = new Handler();

   @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    try {

        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub


                finish();
                Intent i = new Intent(Asplash.this, Example.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(i);
            }
        }, 3000);

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

}




  @Override
protected void onPause() {
    // TODO Auto-generated method stub
     handler.removeCallbacksAndMessages(null);
       finish();
    super.onPause();

}
}

这是清单文件:

     <activity android:name=".Asplash"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
        android:configChanges="orientation">
        <intent-filter >
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>


    <activity
        android:name="com.example.Example"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

我只想制作这个“Asplash”活动,以横向和纵向显示。我还尝试在 LAYOUT 和 LAYOUT-LAND 文件夹中为“splash”创建 XML 文件。然后也是同样的恐慌......

实际上在 ANDROID 中,它应该像基本示例一样自动调整 ORIENTATION 变化。但我不明白为什么它在这里不起作用......

【问题讨论】:

标签: android splash-screen screen-orientation


【解决方案1】:

使用此代码:

public class Asplash extends Activity{

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        startLoading();
    }
    //Start new activity and finish splash activity
    public void openMainActivity() {
        Intent i = new Intent(Asplash.this, Example.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
         startActivity(i);
         finish();
    }
    //Start thread which will wait for 3 secs and call openMainActivity
    private void startLoading(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                }
                openMainActivity();
            }
        }).start();
    }
}

finish() 应该在打开新活动后调用。并尝试此代码,它将解决您的问题。您无需对清单进行任何更改。

【讨论】:

  • 不管是什么方法,最后这个方法帮我搞定了。,但我也在尝试我的方法,谢谢VIJAY。
【解决方案2】:

尝试在你的manifest 中放置一个下面的activity,你得到了错误:

   android:configChanges="orientation"

正如Android Documentation 所说:

android:configChanges

列出activity 将自行处理的配置更改。当运行时发生配置更改时,activity 默认关闭并重新启动,但使用该属性声明配置将阻止活动重新启动。相反,activity 保持运行并调用其 onConfigurationChanged() 方法。

【讨论】:

  • 现在看来是完整的答案了。
  • 是的,现在它已经完成了,但它是通常的不得已的解决方案,禁止改变方向,这不是很酷。此外,OP 稍后将在他的应用程序中再次面对这种现象,启动画面是开始了解如何真正处理此问题的好地方。
【解决方案3】:

尝试在android清单文件中添加

    android:configChanges="orientation|screenSize".

但是,如果您的应用程序以 API 级别 12 或更低级别为目标,那么您的 Activity 始终会自行处理此配置更改(此配置更改不会重新启动您的 Activity,即使在 Android 3.2 或更高版本的设备上运行时也是如此)。

【讨论】:

    【解决方案4】:

    在这种情况下我不确定,但是如果您显示的代码是完整的,那么您的 Runnable 的上下文在它运行时已经消失,因为您的 Activity 在此期间已被销毁并重新创建,原因是方向改变。

    Activity 创建 -> 创建 Runnable 作为上下文 -> 方向改变 -> Activity 被销毁 -> Activity 被创建 -> Runnable 正在执行 -> Runnable 的上下文对象无效

    要在这个非常简单的情况下解决这个问题,我认为您可以使用 Application 对象作为 RUNnable 的上下文。

    但是,请注意您还有另一个问题,即重新创建您的 Activity 会启动另一个 Runnable,它会做同样的事情,只是稍后。

    这种低复杂度场景的简单解决方案可能是将对 Runnable 的引用存储在派生的 Application 对象中并检查其是否存在。

    【讨论】:

      【解决方案5】:

      尝试在您的清单中为您遇到错误的活动放置以下一个:

         android:configChanges="orientation"
      
      to the activity tag and then override onConfigurationChanged method and do something like this 
      
          public void onConfigurationChanged(Configuration newConfig) {
              // TODO Auto-generated method stub
              super.onConfigurationChanged(newConfig);
              if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
              {
      
                        load your xml file for landscape
      }
      
      
      else
      {
       load your xml file for potrait
      }
      

      【讨论】:

      • 尝试只在布局文件夹中保留具有不同名称的横向文件
      • 还使用这些单独的文件名调用 setContentView,您必须在布局中保留同名文件,在布局域中保留另一个文件,这样它就不起作用了
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多