2014-04-01 38 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`

相关问题