启动画面持续5秒后,下一页未运行

时间:2014-04-20 17:46:58

标签: android

我为启动画面运行以下代码5秒钟。

在早期版本的ADT中,它正在运行。但现在它无法正常工作,请告诉我代码中的问题是什么。启动画面持续5秒后,下一页未运行。我希望在第二个名为 FirstScreen.class 的屏幕后运行启动画面5秒钟。

   package com.abc;

   import android.support.v7.app.ActionBarActivity;
   import android.support.v4.app.Fragment;
   import android.content.Intent;
   import android.os.Bundle;
   import android.view.LayoutInflater;
   import android.view.Menu;
   import android.view.MenuItem;
   import android.view.View;
   import android.view.ViewGroup;

   public class MainActivity extends ActionBarActivity {

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

        Thread background = new Thread()
        {
               public void run() 
                        {
                            try {
                                    sleep(5*1000);
                                            Intent i=new Intent(getBaseContext(),FirstScreen.class);
                                    startActivity(i);
                                    finish();
                                } catch (Exception e) 
                                {

                                }
                        }
        };
        background.start();
    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
   }

  } 

1 个答案:

答案 0 :(得分:0)

请使用此代码,让我知道它是否有效。

new Handler().postDelayed(new Runnable() {

       @Override
        public void run() {
            // This method will be executed once the 5 seconds timer is over
            // Start your application's main activity
            Intent i=new Intent(MainActivity.this),FirstScreen.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, 5000);