简体   繁体   English

startActivity()不显示新活动

[英]startActivity() doesn't display new activity

I'am new to android , so please bear with me. 我是android的新手,所以请多多包涵。 I'am trying to display another activity after a timer of 5 seconds, but it doesn't show up. 我正在尝试在5秒钟的计时器后显示另一项活动,但它没有显示。 I've made sure both the activities are in the manifest file. 我确保这两个活动都在清单文件中。

Splash.java : 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 : 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 : 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>

Add timer.start(); 添加timer.start(); as

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

add timer.start() 添加timer.start()

Once you create a thread , you need to start it too ^^ 创建线程后,也需要启动它^^

public void run() {

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

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


    }
};

timer.start();

You can create handler as well instead thread as follows,and make sure you have to finish splash activity as it is splash screen 您还可以按照以下方式创建处理程序,而不是线程,并确保必须完成启动活动,因为它是启动屏幕

 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);

remove timer thread and add this code 删除计时器线程并添加此代码

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

You just forgot to start the thread. 您只是忘记了启动线程。 So just call timer.start(); 因此,只需调用timer.start();

Add below code to your Splash.java class. 将以下代码添加到您的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();
}
}

try like this 这样尝试

in splash Activity class: 在启动活动类中:

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

    setContentView(R.layout.activity_splash);

    getSplash();
}

In getSplash() method : 在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();
}

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

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