android启动画面错误

时间:2013-07-27 13:23:47

标签: android screen splash

我尝试将启动画面添加到我的应用程序,但是当它从启动转移到主要停止时,当我尝试将其保留在其他页面时显示相同

这是我的代码

splashscreen.java

public class SplashScreen extends Activity {
    ImageView splashimg;
    AlphaAnimation animation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.splash);

        //Set up fade in animation
        animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(1000);
        splashimg = (ImageView) findViewById(R.id.imageView1);
        splashimg.setAnimation(animation);

        // Thread to waste time while displaying splash screen
        Thread SplashThread = new Thread() {
            @Override
            public void run() {
                try {
                    synchronized (this) {
                        // Wait given period of time
                        wait(3000);
                    }
                } catch (InterruptedException ex) {
                }

                finish();

                // Run next activity
                Intent intent = new Intent();
                intent.setClass(SplashScreen.this, Main.class);
                startActivity(intent);

                //Terminate splash screen
                SplashScreen.this.finish();

            }
        };

        SplashThread.start();
    }

}

mainactivity.java

public class MainActivity extends SherlockFragmentActivity {

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

        ActionBar actionbar = getSupportActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionbar.setTitle("Bahrain Cinema");

        ActionBar.Tab Frag1Tab = actionbar.newTab().setText("Home");
        ActionBar.Tab Frag2Tab = actionbar.newTab().setText("Showing now");
        ActionBar.Tab Frag3Tab = actionbar.newTab().setText("Coming soon");
        ActionBar.Tab Frag4Tab = actionbar.newTab().setText("Cinema locator");

        Fragment Fragment1 = new Home();
        Fragment Fragment2 = new Showing_now();
        Fragment Fragment3 = new Coming_soon();
        Fragment Fragment4 = new Cinema_locator();

        Frag1Tab.setTabListener(new MyTabsListener(Fragment1));
        Frag2Tab.setTabListener(new MyTabsListener(Fragment2));
        Frag3Tab.setTabListener(new MyTabsListener(Fragment3));
        Frag4Tab.setTabListener(new MyTabsListener(Fragment4));

        actionbar.addTab(Frag1Tab);
        actionbar.addTab(Frag2Tab);
        actionbar.addTab(Frag3Tab);
        actionbar.addTab(Frag4Tab);

    }

    class MyTabsListener implements ActionBar.TabListener {
        public Fragment fragment;

        public MyTabsListener(Fragment fragment){
            this.fragment = fragment;
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            ft.replace(R.id.fragment_container, fragment);
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }
    }

}

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

我认为问题在于调用

finish();

在致电startActivity之前。尝试删除它。如果这不起作用,那么发布logcat是否崩溃。

       finish();   // Remove this line. You are finishing it below the Intent

        // Run next activity
        Intent intent = new Intent();
        intent.setClass(SplashScreen.this, Main.class);
        startActivity(intent);

        //Terminate splash screen
         SplashScreen.this.finish();

答案 1 :(得分:0)

我认为主要的问题是你没有尝试开始正确的活动:

intent.setClass(SplashScreen.this, Main.class); //<--- Shouldn't this be MainActivity.class?

您还要拨打finish()两次。

最后,您可以使用Handler来安排延迟事件(而不是使用线程):

Handler handler = new Handler();
handler.postDelayed(runnable, 3000);

答案 2 :(得分:0)

问题出在androidmanifest.xml文件中。

我应该添加添加主要活动的活动