2013-04-26 50 views
0

工作我寫了飛濺Screeen在應用閃屏不與螺紋

public class SplashScreen extends Activity { 

ImageView imgView; 
int[] imgID = new int[]{R.drawable.frame0, R.drawable.frame1, R.drawable.frame2, R.drawable.frame3, 
     R.drawable.frame4, R.drawable.frame5, R.drawable.frame6}; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 

    imgView = (ImageView) findViewById(R.id.imgSplash); 

    new Thread(new WelcomeScreen()).start(); 

} 

private class WelcomeScreen implements Runnable { 

    @Override 
    public void run() { 


      try { 
       for (int i = 0; i < imgID.length; i++) 
       { 
        imgView.setImageResource(imgID[i]); 
        sleep(500); 
       } 

      } catch (InterruptedException e) { 

      }finally { 
       Intent intent = new Intent(SplashScreen.this,LoginActivity.class); 
       startActivity(intent); 
       finish(); 
      } 


    } 
} 

}

它得到錯誤「對不起應用程序意外終止」的開機時間運行。我不知道爲什麼。有人可以幫助我?

+0

後與異常的堆棧跟蹤... – Darwind 2013-04-26 10:15:13

+0

安置自己的logcat跟蹤 – Pragnani 2013-04-26 10:18:10

回答

0

您不能在與UI線程不同的線程內設置yuor ImageView的資源。 您可以使用runOnUiThread。它將參數作爲可運行參數,並將其發佈到UI線程隊列中。在那裏,UIad需要它並更新你的ImageView。總而言之,您的runnable將變爲:

private class WelcomeScreen implements Runnable { 

@Override 
public void run() { 


     try { 
      for (int i = 0; i < imgID.length; i++) 
      { 
       final int resuorceId = imgID[i]; 
       runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
         imgView.setImageResource(resuorceId); 
         } 
       }); 

       sleep(500); 
      } 

     } catch (InterruptedException e) { 

     }finally { 
      Intent intent = new Intent(SplashScreen.this,LoginActivity.class); 
      startActivity(intent); 
      finish(); 
     } 


} 
+0

我怎麼能解決??? – 2013-04-26 10:18:04

+0

它顯示空白的手段? – Blackbelt 2013-04-26 10:30:27

+1

它正在工作。非常感謝你:) – 2013-04-26 10:36:41

0

您無法從主題訪問您的視圖。 你需要把你的代碼imgView.setImageResource(imgID [i]);在runOnUiThread

使用,如:

runOnUiThread(新的Runnable(){

 @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      imgView.setImageResource(imgID[i]); 
     } 
    }); 

感謝

0

不能在UI從非UI線程改變一些東西,以便替換此你的代碼:

imgView.setImageResource(imgID[i]); 

到:

runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     imgView.setImageResource(imgID[i]); 
    } 
}); 
+0

我的想法是通過使用線程使動畫啓動畫面。我嘗試使用AnimationDrawable,但它的工作。我設置圖像資源並延遲一段時間。但它會得到錯誤 – 2013-04-26 10:23:47

+0

@Nam Nguyen這只是一個代碼中的明顯錯誤,如果你有更多的異常消息,你應該發佈所有這些消息以獲得更多幫助 – Evos 2013-04-26 10:27:46

0
//try code this way... 
public class SplashScreen extends Activity { 

private Intent launchIntent; 
private Thread splashThread;     //used for perform splash screen operation 

private int splashTime = 10000, sleepTime = 50; //used for threading operation 
private boolean active = true;     //used for touch event 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splashscreen);         //Set splashscreen.xml here 

    try {   
      splashThread = new Thread() {         // Creating Thread for splash the screen 

      @Override 
      public void run() {            // run method implemented to perform threading operation 
       try { 
        int waitTime = 0;          //counter for threading 
        do { 
         sleep(sleepTime);         //delay for specific time 
         if (active) 
          waitTime += 100; 


        //write your image code here that display your no. of images 



        } while (active && (waitTime < splashTime));   //Check touch condition and counter 

       } catch (Exception e) { 
        // to handle runtime error of run method       
        Validation.displayToastMessage(SplashScreen.this, e.toString()); //Call static method of class ToastMessage 
       } 
       finish();                //finish current activity 
       startJustCoupleActivityScreen();          //Call below defined function 
      } 
     }; 
     splashThread.start();               //start thread here 
    } catch (Exception e) { 
     message("SplashScreen : "+ e.toString());    //Call static method of class ToastMessage 
    } 

} 

public void startJustCoupleActivityScreen() { 
    launchIntent=new Intent(SplashScreen.this,JustCoupleActivity.class); //call Next Screen 
    startActivity(launchIntent);           //start new activity 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) {       //onTouch Event 
    //on touch it immediate skip splash screen 
    if(event.getAction()==MotionEvent.ACTION_DOWN) active=false;   //Check Touch happened or not 
    return true; 
} 

public void message(String msg) 
{ 
    Validation.displayToastMessage(SplashScreen.this, msg);  //display Error Message 
} 

}