2012-11-07 10 views
5

ho un asyncTaskProc che leggere alcune informazioni da un DB e scrivere sul ui ...Android: perché ottengo questi errori AsyncTask?

il codice funziona perfectcly su Android 4.0, ma non funziona su 2.3 ... ecco il codice

NUOVO AsyncTask

`IceCastPoll public class estende TimerTask {

public IceCastPoll() { 

    } 

    @TargetApi(9) 
    public void run() { 

     new AsyncTaskProc().execute(); 
    } 

}` 

l'attuazione AsyncTask

01.235.164,106 mila
@TargetApi(9) 
class AsyncTaskProc extends AsyncTask<Void, String, Void> { 
    List<Stream> streams=null; 

    @Override 
    protected void onPostExecute(Void result) { 

     textSong =(TextView) findViewById(R.id.textViewCurrentSong); 
     textArtist =(TextView) findViewById(R.id.textViewCurrentArtist); 
     textTit=(TextView) findViewById(R.id.textViewTit); 
     textArt=(TextView) findViewById(R.id.TextViewArt); 
     copertina=(ImageView) findViewById(R.id.imageViewCopertina); 
     new DownloadImageTask((ImageView) findViewById(R.id.imageViewCopertina)).execute("http://service.uniradiocesena.it/OnAir.jpg"); 

     try { 
      for (Stream stream: streams) { 

       try 
       { 
        //Thread.sleep(5000); 
        //textSong.setText((stream.getCurrentSong())); 
        textArt.setText("Artista:"); 
        textTit.setText("Titolo:"); 
        StringTokenizer tokens = new StringTokenizer(stream.getCurrentSong(), "-"); 
        String first = tokens.nextToken(); 
        String second = tokens.nextToken(); 
        textSong.setText(first); 
        textArtist.setText(second); 

       } catch (Exception e) { 
        //Thread.sleep(5000); 
        textSong.setText((stream.getCurrentSong())); 
        textArt.setText("Rotazione:"); 
        textTit.setText(""); 
        textArtist.setText(""); 
       } 
      } 
     } catch (Exception e) { 

      //Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show(); 

     } 

    } 

    @Override 
    protected Void doInBackground(Void... unused) { 



     Scraper scraper = new IceCastScraper(); 

     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 


     try { 
      streams = scraper.scrape(new URI("http://r35798.ovh.net:8000/")); 

     } catch (ScrapeException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      //Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show(); 
     } catch (URISyntaxException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      //Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show(); 
     } 




     return (null); 
    } 


}` 

GLI ERRORI Logcat

11-07 11:09:49.729: W/dalvikvm(18983): Exception Ljava/lang/RuntimeException; thrown while initializing Landroid/os/AsyncTask; 
11-07 11:09:49.729: W/dalvikvm(18983): threadid=9: thread exiting with uncaught exception (group=0x40018560) 
11-07 11:09:49.739: E/AndroidRuntime(18983): FATAL EXCEPTION: Timer-0 
11-07 11:09:49.739: E/AndroidRuntime(18983): java.lang.ExceptionInInitializerError 
11-07 11:09:49.739: E/AndroidRuntime(18983): at com.example.appuniradiocesena.SwipeyTabsSampleActivity$IceCastPoll.run(SwipeyTabsSampleActivity.java:233) 
11-07 11:09:49.739: E/AndroidRuntime(18983): at  java.util.Timer$TimerImpl.run(Timer.java:284) 
11-07 11:09:49.739: E/AndroidRuntime(18983): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
11-07 11:09:49.739: E/AndroidRuntime(18983): at android.os.Handler.<init>(Handler.java:121) 
11-07 11:09:49.739: E/AndroidRuntime(18983): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421) 
11-07 11:09:49.739: E/AndroidRuntime(18983): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421) 
11-07 11:09:49.739: E/AndroidRuntime(18983): at android.os.AsyncTask.<clinit>(AsyncTask.java:152) 
11-07 11:09:49.739: E/AndroidRuntime(18983): ... 2 more 

Ogni suggerimento sarà molto molto apprezzata!

e sry per errori inglese: D

+0

perché mai si vuole eseguire un AsyncTask da un filo? – njzk2

risposta

9

È necessario avviare AsyncTask con uno Handler creato sul thread principale. Quindi sostituire il metodo di run() in IceCastPoll con questo:

private Handler handler = new Handler(Looper.getMainLooper()); 

@Override 
public void run() { 
    handler.post(new Runnable() { 
     public void run() { 
      new AsyncTaskProc().execute(); 
     } 
    }); 
} 
+0

awsome ... che funziona perfettamente! TY uomo! – radudac

1
new AsyncTaskProc().execute();  

Non mettere questo codice all'interno di un thread.

Perché è già un gestore.

Semplice Esegui il codice ogni volta che ti serve come nuovo AsyncTaskProc(). Execute(); è abbastanza mio amico

0

Un suggerimento rapido nel codice: Perché stai facendo findViewById() ogni volta, perché stai cercando di eseguire AsyncTask a intervalli particolari, quindi il suo modo non fattibile per farlo ogni volta.

Invece, trova tutte quelle viste all'interno del metodo onCreate().

+0

ty per il suggerimento ... farò come hai detto tu – radudac

Problemi correlati