2012-07-19 45 views
0

我遇到與我的應用程序4.x設備的問題。它看起來像一個線程正在使其崩潰,同時改變活動(從啓動畫面到實際應用程序)。下面是截圖:Android:UnsupportedOperationException與線程

UnsupportedOperationException screenshot

飛濺活動代碼:

public class Splash extends Activity { 

protected boolean _active = true; 
protected int _splashTime = 3000; // tempo di permanenza spash screen 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 

    Thread splashTread = new Thread() { 
     @Override 
     public void run() { 
      try { 
       int waited = 0; 
       while(_active && (waited < _splashTime)) { 
        sleep(100); 
        if(_active) { 
         waited += 100; 
        } 
       } 
      } catch(InterruptedException e) { 
       // do nothing 
      } finally { 
       finish(); 
       Intent i = new Intent(Splash.this, Test01Activity.class); 
       startActivity(i); 
       stop(); 
      } 
     } 
    }; 
splashTread.start(); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     _active = false; 
    } 
    return true; 
} 
} 

此外,應用好好嘗試一下真正的崩潰,作爲主要的活動一直在後臺運行(後面的「不幸的是,應用程序已停止工作「警報)。這個問題只發現在4.x設備上,2.x和3.x全部正常。錯誤是在第37行。

+0

究竟哪裏崩潰?請粘貼您的LogCat。 – 2012-07-19 08:37:08

+0

在哪一行崩潰 – 2012-07-19 08:38:18

+0

您的代碼似乎與此主題中的代碼相同:http://stackoverflow.com/q/4414737/517561 – Sparky 2012-07-19 08:40:13

回答

2

我認爲例外是由於使用deprecated stop method of Thread class。它可能不支持ICS 4.0

您可以使用Handler而不是您的目的的線程。

你可以試試下面的代碼閃屏內onCreate方法

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 

    Handler mHandler = new Handler(); 
     mHandler.postDelayed(new Runnable() { 

      @Override 
      public void run() { 
       Intent i = new Intent(Splash.this, Test01Activity.class); 
       startActivity(i); 
      finish(); 

      } 
     }, _splashTime); 

} 
+0

我設法刪除停止(),它一切工作。謝謝 – Stefano 2012-07-19 09:32:54

0

與GUI的每個操作都必須在事件UI線程被起訴。 Android 4在這個要求中更加嚴厲。這可能是你的應用崩潰的原因。正如我所看到的,您不會更改UI線程中的活動。有關詳細信息,請參閱AsyncTask