2011-10-06 5 views
2

Quelqu'un sait pourquoi AsyncTask est la fermeture de la force lorsque je tente de décompresser un fichier en arrière-plan, il fonctionne quand ne pas utiliser AsyncTask donc je ne suis pas sûr de ce que je fais mal:AsyncTask Force Close

public class UnzipFile extends AsyncTask<Void, Integer, String> {               
String zipFile = Environment.getExternalStorageDirectory() + "/download/" + mixtapefilename; 
String unzipLocation = Environment.getExternalStorageDirectory() + "/download/"; 

    @Override 
    protected void onPreExecute() {   
     Toast toast = Toast.makeText(getApplicationContext(), "Unzipping...", Toast.LENGTH_SHORT); 
     toast.show(); 

     /* Intent intent = new Intent(); 
     final PendingIntent pendingIntent = PendingIntent.getActivity(Download.this, 0, intent, 0); 

     // configure the notification 
     notification = new Notification(R.drawable.zipicon, "Unzipping...", System 
       .currentTimeMillis()); 
     notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; 
     notification.setLatestEventInfo(getApplicationContext(), "Please Wait", "Unzipping...", pendingIntent); 
     notificationManager2.notify(1, notification);*/ 

    } 

    @Override 
    protected String doInBackground(Void... params) {       
     Decompress d = new Decompress(zipFile, unzipLocation); 
     d.unzip();    

     return "success";}        

    @Override 
    protected void onPostExecute(String result) { 
     Toast toast = Toast.makeText(getApplicationContext(), "Download Complete", Toast.LENGTH_SHORT); 
     toast.show(); 
     File oldzip = new File(zipFile); 
     boolean deleted = oldzip.delete(); 
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
     /*notificationManager2.cancel(1);*/        
    } 
}  

et alors voici ma partie Décompressez que je suis en train de courir en arrière-plan

public class Decompress { 
     byte[] buffer = new byte[1024]; 
     int length; 
     private String _zipFile; 
     private String _location; 

     public Decompress(String zipFile, String location) { 
     _zipFile = zipFile; 
     _location = location; 
     File a = new File(_location + foldername); 
     if(!a.isDirectory()){ 
      a.mkdirs(); 
     }       

     _dirChecker(""); 
     } 

     public void unzip() { 

     try { 
      FileInputStream fin = new FileInputStream(_zipFile); 
      ZipInputStream zin = new ZipInputStream(fin); 
      ZipEntry ze = null; 
      while ((ze = zin.getNextEntry()) != null) { 


      if(ze.isDirectory()) { 
       _dirChecker(ze.getName()); 
      } else { 
       FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
       while ((length = zin.read(buffer))>0) { 
        fout.write(buffer, 0, length); 
        } 

       zin.closeEntry(); 
       fout.close(); 
      } 

      } 
      zin.close(); 
      Toast toast = Toast.makeText(getApplicationContext(),"Download Complete", Toast.LENGTH_LONG); 
      toast.show(); 
     } catch(Exception e) { 
      ProgressDialog dialog; 
     dialog = new ProgressDialog(Download.this);     
     dialog.setMessage(e.toString()); 
     dialog.show(); 
     } 

     } 

     private void _dirChecker(String dir) { 
     File f = new File(_location + dir); 


     if(!f.isDirectory()) { 
      f.mkdirs(); 
     } 
     } 
    } 
+0

Quelle est l'exception que logcat crache? –

Répondre

1

Puisque vous utilisez AsynTask vous ne pouvez pas communiquer avec le thread principal de l'interface utilisateur (c.-à-messages de pain grillé d'affichage) à l'intérieur doInBackground().

La seule façon de communiquer avec le thread principal de l'interface utilisateur pendant AsynTask est à l'aide onPreExcute() ou onPostExcute()

Donc il suffit de supprimer cette ligne:

Toast toast = Toast.makeText(getApplicationContext(),"Download Complete", Toast.LENGTH_LONG); 
+0

Merci, je l'ai compris juste après avoir frappé la poste. Désolé pour la question inutile. –

0

Je suis désolé d'être en désaccord, mais il y a plusieurs façons pour exécuter du code dans le thread UI à partir d'un autre thread: Activity.runOnUiThread (Runnable) View.post (Runnable) View.postDelayed (Runnable, long) vous pourriez utiliser un de ceux pour poster quelque chose qui sera exe coupée par le Thread UI quel que soit le thread dans lequel vous êtes actuellement. Mais oui vous pouvez simplement les enlever et utiliser le Log.d (String, String) à la place (par exemple)

0

UI Thread est non threadable. Il est donc très dangereux d'accéder au thread UI dans un autre thread. Vous devez publier votre message dans la file d'attente des messages de l'interface utilisateur par la méthode View.post()

Questions connexes