2014-04-01 64 views
0

我是android新手。我有兩個活動Splash和MainActivity。當我啓動我的應用程序時,Splash活動開始(如其預期的那樣),它播放聲音,但背景圖像不出現,幾秒鐘後我的MainActivity啓動。提前致謝!飛濺類未出現第一個活動視圖

package com.example.button; 
import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 

public class Splash extends Activity{ 
MediaPlayer ourSong; 
Thread timer; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
      setContentView(R.layout.splash); 
    ourSong=MediaPlayer.create(Splash.this,R.raw.addicted); 
    ourSong.start(); 
      timer=new Thread(); 
      timer.start();      
      run(); 
      //{ 
         //}; 


    } 

    public void run() 
    { 

     try { 


      timer.sleep(5000); 

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

     }finally { 
Intent openStartingPoint=new Intent("com.example.button.MAINACTIVITY"); 
      startActivity(openStartingPoint); 
     } 
     }    




    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
ourSong.release(); 
    } 





} 

代碼MainActivity類別

代碼

Package com.example.button; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    int counter; 
    Button add; 
    Button sub; 
    TextView display; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    counter=0; 
    add=(Button)findViewById(R.id.button1); 
    sub=(Button)findViewById(R.id.button2); 
    display=(TextView)findViewById(R.id.textView1); 
    } 


    @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; 
    } 
public void add(View view) 
{ 
    counter=counter+1; 
    display.setText("your total is"+counter); 
} 

public void sub(View view) 
{ 
    counter--; 
    display.setText("your total is"+counter); 
} 

} 

代碼splash.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="@drawable/feather"> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 

</LinearLayout> 
+0

佈局後 - splash.xml – user2450263

+0

哪裏是你的形象? –

+0

我已經發布了上面的splash.xml代碼。請看看它。謝謝 – user3485978

回答

0

我想你沒有正確使用線程。改變這些三行代碼

timer=new Thread(); 
    timer.start();      
    run(); 

timer=new Thread(new Runnable(){ 
     public void run() { 
      try { 
       sleep(5000); 
      }catch(InterruptedException e){ 
       e.printStackTrace(); 
      }finally { 
       Intent openStartingPoint=new Intent("com.example.button.MAINACTIVITY"); 
       startActivity(openStartingPoint); 
      } 
     }    
    }); 
    timer.start(); 

,並從活動中刪除run方法。 通過這種方式,您可以讓飛濺活動在屏幕上顯示5秒鐘。

0

使用此代碼爲您的飛濺活動:

public class SplashActivity extends Activity { 
    int SPLASH_DISPLAY_LENGHT = 1000; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.splash_form); 

     InitilizeUi(); 
    } 

    private void InitilizeUi() { 
     // play your sound 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       Intent mainIntent = new Intent(SplashActivity.this, AboutActivity.class); 
       SplashActivity.this.startActivity(mainIntent); 
       SplashActivity.this.finish(); 
      } 
     }, SPLASH_DISPLAY_LENGHT); 
    } 
} 
0

在splash.xml採取imageview的,把你的圖片文件存在。

e.g android:src="file.gif"

希望這有助於you`

相關問題