2012-04-04 8 views
5

Ho appena pubblicizzato ieri la mia prima applicazione Android. Non ho provato su Android 4.0 e il mio amico mi ha appena detto che la mia app si blocca sulla sua galassia S2 (4.0.3)La mia app funziona su Android 2.3.3 per Android 3.1 ma si ferma con errore su 4.0 +

Si blocca dopo alcuni secondi nella mia attività di splash screen, solo poche righe di codice forse voi potete controllare:

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

    try 
    { 

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); 
    overridePendingTransition(0 , 0); 

    // thread for displaying the SplashScreen 
    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(); 

       try 
       { 
        /* 
        Intent i = new Intent(); 
        i.setClass(SplashScreen.this, MainActivity.class); 
        startActivity(i); 
        finish(); 
        */ 
       } 
       catch(Exception e) 
       { 
        ki(e); 
       } 

       stop(); 
      } 
     } 
    }; 

    splashTread.start(); 

    } 
    catch(Exception ex) 
    { 
     ki(ex); 
    } 

} 

@Override 
public void onBackPressed() { 
    return; 
} 



//Toaster 
    public void ki(Exception message) 
    { 


    Toast myToast = Toast.makeText(getApplicationContext(), message.toString(), 1); 
    myToast.show(); 

} 

Opere verry bene su Android 2,3-3,1 ma Non riesco a capire che cosa è il problema con la versione successiva alla 4.0

Aiutaci grazie!

Edit:

se elimino il mio filo tutto funziona bene. Quindi la mia nuova domanda è ... Che cosa è nuovo con le discussioni in 4.0? Ho appena eseguito un thread che non fa nulla e anche io ho avuto l'incidente.

+1

Cerca in LogCat! –

+1

Sarebbe molto utile se è possibile condividere LogCat – Naved

+0

http://tinyurl.com/7tyuqbw – weakwire

risposta

5

Ho avuto lo stesso problema utilizzando stop() su Android 4.0. Prova a utilizzare finish(), che ha risolto il mio problema.

+0

Grazie amico che hai fatto la mia giornata. –

+1

Questa risposta è sbagliata. finish() chiude l'attività non interrompe il thread. In questo caso il Thread esce comunque in ogni caso, ma in tutte le altre circostanze il Thread continuerebbe ad esibirsi anche dopo la morte dell'Attività. – Graeme

3

Immagino che stop() non funzioni più su ICS.

Il mio tutorial at droidnova.com non è aggiornato per funzionare su ICS, mi spiace, non ha avuto il tempo per quello. Oggi userei un gestore invece del thread separato. Molto più facile da usare e più robusto.

8

Thread.stop(), resume() e suspend() non funziona più con Android 4.0. Il codice sorgente è qui sotto:

/** 
* Requests the receiver Thread to stop and throw ThreadDeath. The Thread is 
* resumed if it was suspended and awakened if it was sleeping, so that it 
* can proceed to throw ThreadDeath. 
* 
* @deprecated because stopping a thread in this manner is unsafe and can 
* leave your application and the VM in an unpredictable state. 
*/ 
@Deprecated 
public final void stop() { 
    stop(new ThreadDeath()); 
} 

/** 
* Throws {@code UnsupportedOperationException}. 
* 
* @throws NullPointerException if <code>throwable()</code> is 
*   <code>null</code> 
* @deprecated because stopping a thread in this manner is unsafe and can 
* leave your application and the VM in an unpredictable state. 
*/ 
@Deprecated 
public final synchronized void stop(Throwable throwable) { 
    throw new UnsupportedOperationException(); 
} 

Un sacco di crash dell'applicazione su Android 4.0 a causa di questo. Questa non è colpa di Google; da anni fa Java SDK si è scoraggiato usando stop() su un thread.

Citazione di changelog:

Commit: a7ef55258ac71153487357b861c7639d627df82f [a7ef552] 
Author: Elliott Hughes <[email protected]> 
Date: 2011-02-23 6:47:35 GMT+08:00 

Simplify internal libcore logging. 

Expose LOGE and friends for use from Java. This is handy because it lets me use 
printf debugging even when I've broken String or CharsetEncoder or something 
equally critical. It also lets us remove internal use of java.util.logging, 
which is slow and ugly. 

I've also changed Thread.suspend/resume/stop to actually throw 
UnsupportedOperationException rather than just logging one and otherwise 
doing nothing. 

Bug: 3477960 
Change-Id: I0c3f804b1a978bf9911cb4a9bfd90b2466f8798f 
7

come dice @Yuku, Thread.stop() non è in qualche modo rotto in ICS è specificamente cambiato per generare un'eccezione in quanto è pericoloso:

http://developer.android.com/reference/java/lang/Thread.html#stop()

stop vuoto pubblico finale sincronizzato (Lanciabile gettabile)

Dal: API Livello 1 Questo metodo è obsoleto. poiché l'interruzione di un thread in questo modo non è sicuro e può lasciare l'applicazione e la VM in uno stato imprevedibile.

Derivazioni non supportateOperationException. Lanci NullPointerException se Throwable() è nullo

Se volete siete filo da forza smesso di utilizzare invece threadName.interrupt() mentre esterno al thread. Programmare in modo nativo il ciclo di vita dei thread in modo che si arresti in modo naturale quando l'attività è completa.

Nell'esempio è possibile semplicemente eliminare il comando stop() poiché il thread cesserà naturalmente l'esecuzione alla fine del suo metodo run().

EDITfinish() è una chiamata al Activity per finire, non il vostro Thread. Nell'esempio sopra il Thread uscirà naturalmente comunque, ma per favore non essere confuso con l'arresto di un Thread e finendo uno Activity in quanto sono cose molto diverse.