2015-09-08 49 views
1

我剛剛接觸到android,請耐心等待。 我試圖在5秒的計時器後顯示另一個活動,但它不顯示。我已經確定這兩個活動都在清單文件中。startActivity()不顯示新的活動

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

還使用處理器比睡眠線程更好的選擇,我想它的轟動卵石它本身不是一個好主意。您現在可以進行品牌發佈 – Raghunandan

回答

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

添加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(); 
0

嘗試這樣

濺活動類:

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

刪除計時器線程,並添加此代碼

Handler handler = new Handler(); 
       handler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
          startActivity(new Intent(Splash.this,MainActivity.class)); 
         } 
        } 
       }, 5000); 
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(); 
} 
}