简体   繁体   English

Android启动画面错误

[英]android splash screen error

i try to add splash screen to my app but when it transfer from splash to main it stop and when i try to keep it in other pages is showing the same 我尝试将闪屏添加到我的应用程序,但是当它从闪屏转移到主屏时,它停止了,并且当我尝试将其保留在其他页面中时,它也显示相同的内容

here is my code 这是我的代码

splashscreen.java 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 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

        }
    }

}

can anyone help me? 谁能帮我?

I think the problem is calling 我认为问题在于

finish();

before calling startActivity . 在调用startActivity之前。 Try removing that. 尝试删除它。 If that doesn't work then post the logcat if it is crashing. 如果这样不起作用,则在崩溃时发布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();

I think the main problem is that you're not trying to start the right activity: 我认为主要问题是您没有尝试开始正确的活动:

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

You're also calling finish() twice. 您还要两次调用finish()

Finally, you can use a Handler to schedule a delayed event (instead of using Threads): 最后,您可以使用Handler安排延迟事件(而不是使用Threads):

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

The issue was in the androidmanifest.xml file. 问题出在androidmanifest.xml文件中。

I should have added add the activity for main activity 我应该为主要活动添加活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM