startActivity()不显示新活动

时间:2015-09-08 05:51:49

标签: java android android-intent

我是android的新手,所以请耐心等待。 我试图在5秒的计时器后显示另一个活动,但它没有显示出来。我确保这两项活动都在清单文件中。

Splash.java

package com.example.pournima.metallica;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;


public class Splash extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Thread timer = new Thread(){
        public void run() {

        try{
            Thread.sleep(5000);
        }catch(Exception e){

        }finally{
            Intent in = new Intent(Splash.this,MainActivity.class);
            startActivity(in);
        }


        }
    };

}
}

MainActivity.java

package com.example.pournima.metallica;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

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

@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_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();

    //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.pournima.metallica" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.pournima.TheStart" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

6 个答案:

答案 0 :(得分:2)

timer.start();添加为

Thread timer = new Thread(){
        public void run() {

        try{
            Thread.sleep(5000);
        }catch(Exception e){

        }finally{
            Intent in = new Intent(Splash.this,MainActivity.class);
            startActivity(in);
        }


        }
    };

timer.start();

答案 1 :(得分:2)

添加timer.start()

创建线程后,您还需要启动它^^

public void run() {

    try{
        Thread.sleep(5000);
    }catch(Exception e){

    }finally{
        Intent in = new Intent(Splash.this,MainActivity.class);
        startActivity(in);
    }


    }
};

timer.start();

答案 2 :(得分:1)

您也可以创建处理程序,而不是如下所示,并确保您必须完成启动活动,因为它是启动画面

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

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

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

答案 3 :(得分:1)

删除计时器线程并添加此代码

Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                            startActivity(new Intent(Splash.this,MainActivity.class));
                        }
                    }
                }, 5000);

答案 4 :(得分:1)

你只是忘了启动线程。所以只需致电timer.start();

将以下代码添加到您的Splash.java类中。

Thread timer = new Thread(){
        public void run() {

        try{
            Thread.sleep(5000);
        }catch(Exception e){

        }finally{
            Intent in = new Intent(Splash.this,MainActivity.class);
            startActivity(in);
        }


        }
    };
timer.start();
}
}

答案 5 :(得分:0)

试试这个

在splash Activity类中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_splash);

    getSplash();
}

在getSplash()方法中:

private void getSplash() {


    Handler welcome_handler = new Handler();

    Thread thread = new Thread() {
        public void run() {
            try {
                sleep(Constants.SPALASH_TIMMING);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                finish();
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
            }
        }
    };
    thread.start();
}