如何在SplashScreen之后启动另一个活动。在给定的计时器之后,闪屏只会崩溃

时间:2015-01-17 08:02:46

标签: java android android-studio splash-screen

我是一名大学生,我刚刚开始研究android。我的启动画面在其定时限制后崩溃,但下一个活动页面没有显示。我怎么能解决这个问题?

这是我的SplashScreen.java:

public class SplashScreen extends ActionBarActivity {

    private final int SPLASH_DISPLAY_TIMER = 5000;

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

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent mainIntent = new Intent(SplashScreen.this,Menu.class);
                        SplashScreen.this.startActivity(mainIntent);
                        SplashScreen.this.finish();
            }
        },SPLASH_DISPLAY_TIMER);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_splash_screen, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我的loginActivity.java,必须在SplashScreen之后启动:

public class loginActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        Context context = getApplicationContext();
        CharSequence text = "Please fill your login details!";
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        toast.setGravity(Gravity.CENTER,0,0);
        loginActivity.this.startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ambuj.supercabs" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
         >
        <activity
            android:name=".SplashScreen"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".loginActivity"
            android:label="@string/title_activity_login"
            android:parentActivityName=".SplashScreen"
            android:theme="@style/Theme.AppCompat.Light">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.ambuj.supercabs.SplashScreen" />
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

你的行

//错误

Intent mainIntent = new Intent(SplashScreen.this,Menu.class);
                    SplashScreen.this.startActivity(mainIntent);
                    SplashScreen.this.finish();

删除Menu.class并使用loginActivity.class,因为您想重定向到loginActivity

Oncreate()

中的setContentView(R.layout.activity_splash_screen);之后将此代码放入SplashScreen.java
Thread loading = new Thread() {
        public void run() {
            try {
                sleep(5000);    
            Intent main = new   Intent(SplashScreen.this,LoginActivity.class);
          startActivity(main);
          finish();


        }

        catch (Exception e) {
            e.printStackTrace();
        }

        finally {
            finish();
        }
    }
};

loading.start();
}

你的logcat发布后仍有错误。

答案 1 :(得分:0)

删除

Intent mainIntent = new Intent(SplashScreen.this,Menu.class);
                        SplashScreen.this.startActivity(mainIntent);
                        SplashScreen.this.finish();

Intent mainIntent = new Intent(SplashScreen.this,LoginActivity.class);
        startActivity(mainIntent);
       finish();