Android +启动画面在方向更改时崩溃

时间:2013-03-06 12:56:40

标签: android splash-screen screen-orientation

我的应用程序实际上有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&amp ;;中创建用于“splash”的XML文件。 LAYOUT-LAND文件夹。然后也是恐慌......

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

5 个答案:

答案 0 :(得分:4)

使用此代码:

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()应在打开新活动后调用。并尝试此代码它将解决您的问题。您无需对清单进行任何更改。

答案 1 :(得分:0)

尝试将manifest放在activity下面,因为你遇到错误:

   android:configChanges="orientation"

正如Android Documentation所说:

<强>机器人:configChanges

列出activity将自行处理的配置更改。在运行时发生配置更改时,默认情况下会关闭并重新启动activity,但声明具有此属性的配置将阻止活动重新启动。相反,activity仍在运行,并调用其onConfigurationChanged()方法。

答案 2 :(得分:0)

尝试添加android清单文件

    android:configChanges="orientation|screenSize".

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

答案 3 :(得分:0)

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

创建的活动 - &gt;创建Runnable作为上下文 - &gt;方向改变 - &gt;活动被破坏 - &gt;活动已创建 - &gt;正在执行Runnable - &gt; runnable的Context对象无效

为了解决这个非常简单的问题,我认为你可以使用Application对象作为RUnnable的Context。

但请注意,您还有另一个问题,即重新创建您的Activity会启动另一个Runnable,它将在稍后执行相同操作。

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

答案 4 :(得分:0)

尝试将清单中的一个放在您收到错误的活动中:

   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
}