0

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

这是我的代码

闪屏.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();
    }

}

主要活动.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

        }
    }

}

谁能帮我?

4

3 回答 3

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();
于 2013-07-27T14:04:42.293 回答
0

问题出在 androidmanifest.xml 文件中。

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

于 2013-07-27T23:36:31.590 回答
0

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

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

你也打finish()了两次电话。

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

Handler handler = new Handler();
handler.postDelayed(runnable, 3000);
于 2013-07-27T14:11:41.170 回答