2012-03-15 10 views
7

Ciao ho un problema con il mio codice code..my èjava.lang.RuntimeException: Impossibile creare gestore filettatura interna che non ha chiamato Looper.prepare()

progressD = ProgressDialog.show(MenuUtama.this, "", "Uploading files to server.....", false); 
Thread thread = new Thread(new Runnable(){ 
     public void run(){ 
      //doFileUpload(); 
      try { 
      // setiap parameter yang akan dikirim melalui http 
      // harus encode agar 
      // dapat terbaca dengan baik oleh server 
      Cursor c = helper.getAll1(almagId); 
      Cursor cr = helper.getUpImage(almagId); 
       if(c.moveToFirst()){ 
        //progressD = ProgressDialog.show(context, title, message) 
         do{ 
          String kdstore = URLEncoder.encode(helper.getKdStore(c).toString(), "utf-8"); 
          String nama = URLEncoder.encode(helper.getNama(c).toString(), "utf-8"); 
          String alamat = URLEncoder.encode(helper.getAlamat(c).toString(), "utf-8"); 
          String kdpos = URLEncoder.encode(helper.getKdPos(c).toString(), "utf-8"); 
          String notelp = URLEncoder.encode(helper.getNotel(c).toString(), "utf-8"); 
          String lng = URLEncoder.encode(helper.getlng(c).toString(), "utf-8"); 
          String lat = URLEncoder.encode(helper.getLat(c).toString(), "utf-8"); 
          String perush = URLEncoder.encode(helper.getPerus(c).toString(), "utf-8"); 
          //String gambar = URLEncoder.encode(helper.getGamb(c).toString(), "utf-8"); 

          //Toast.makeText(this, kdstore, Toast.LENGTH_LONG).show(); 
          //System.out.println(gambar); 
          url += "?kode_toko=" + kdstore + "&&nama=" + nama + "&&alamat=" + alamat + 
          "&&kode_pos=" + kdpos + "&&no_telp=" + notelp + "&&longitude=" + lng + "&&latitude=" + lat + 
          "&&perusahaan=" + perush; 
          getRequest(url); 
          url = "http://10.234.165.232/upload_get.php"; 
         }while(c.moveToNext()); 
      } 
      if(cr.moveToFirst()){ 
       do{ 
        String kdstore = URLEncoder.encode(helper.getKdstore1(cr), "utf-8"); 
        String gambar = URLEncoder.encode(helper.getGam1(cr), "utf-8"); 
        url1 += "?kode_toko1=" + kdstore + "&&gambar1=" + gambar; 
        getRequest1(url1); 
        url1 = "http://10.234.165.232/upload_get2.php"; 
       }while(cr.moveToNext()); 
      } 

      } catch (UnsupportedEncodingException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      MenuUtama.this.runOnUiThread(new Runnable(){ 
       public void run() { 
        if(progressD.isShowing()) 
         progressD.dismiss(); 
       } 
      }); 
     } 
}); 
thread.start(); 
return(true); 

e errore come questo:

FATAL EXCEPTION: Thread-9 
java.lang.RuntimeException: 
Can't create handler inside thread that has not called Looper.prepare() 
    at android.os.Handler.<init>(Handler.java:121) 
    at android.widget.Toast.<init>(Toast.java:68) 
    at android.widget.Toast.makeText(Toast.java:231) 
    at com.sat.alfaloc.MenuUtama.getRequest(MenuUtama.java:160) 
    at com.sat.alfaloc.MenuUtama$1.run(MenuUtama.java:101) 
    at java.lang.Thread.run(Thread.java:1096) 

se l'attività salvare i dati al server comanderò la barra di avanzamento può essere eseguito, ma se non non è questo worked..what devo fare per risolvere questo problema ??

risposta

13

Probabilmente si sta verificando un errore a causa del thread in cui si sta utilizzando il contesto dell'attività.

È necessario utilizzare AsyncTask anziché un thread normale. In AsyncTask c'è un metodo onPreExecute() e onPostExecute() che sono in esecuzione sul thread principale e c'è un metodo doInBackground() che verrà eseguito sullo sfondo in modo da poter facilmente implementare il processo di lunga durata.

È possibile fare riferimento this example

4

Esegue l'azione specificata sul thread UI

ActivityName.runOnUiThread(new Runnable() { 
    public void run() { 
     //put your code here 
    } 
}); 
5

A volte si potrebbe verificare questo problema anche quando il codice incriminato è all'interno della attività (come nel mio caso, quando con Unity motore di gioco).

Il seguente codice ha funzionato per me.

 this.runOnUiThread(new Runnable() { 
      public void run() { 
       //Your code here 
       ... 
      } 
     }); 
2

Quando si utilizza un AsyncTask e si eseguono processi del filo doInBackground non è possibile eseguire funzioni come toast e, per esempio, l'apertura di un nuovo frammento.

Quando si verifica questo particolare errore, provare a utilizzare questo codice per creare un nuovo thread su cui eseguire il codice. Lo porterà in primo piano, piuttosto che un processo in background (per quanto ho capito).

Per Fragments:

getActivity().runOnUiThread(new Runnable() 
{ 
    public void run() 
    { 
     // Send Toast 
     Toast.makeText(getActivity(), "Your Message!", Toast.LENGTH_SHORT).show(); 
    } 
}); 

Per attività:

this.runOnUiThread(new Runnable() 
{ 
    public void run() 
    { 
     // Send Toast 
     Toast.makeText(this, "Your Message!", Toast.LENGTH_SHORT).show(); 
    } 
}); 

La differenza tra i due è solo se si sta chiamando this, o getActivity() per eseguire il thread su. In entrambi i casi, entrambi creano un nuovo thread sull'attività in esecuzione.

Spero che questo aiuti!

Oh, e come una nota, nella AsyncTask si possono creare le proprie funzioni, come ad esempio:

protected void myTaskFunction() throws exception 
{ 
    // Stuff to do 
} 

In queste funzioni, che fanno parte della classe AsyncTask, questo è dove ci si mette quel pezzo di codice runOnUiThread(). Spero che sia più chiaro.

Problemi correlati