2012-03-15 9 views
7

हाय मैं मेरी code..my कोड के साथ एक समस्या हैjava.lang.RuntimeException: धागा कि 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); 

और इस तरह त्रुटि है:

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) 

अगर गतिविधि को बचाने मैं आदेश सर्वर में डेटा प्रगति बार चला सकते हैं, लेकिन यदि नहीं यह नहीं मैं worked..what इस समस्या को दूर करने के लिए क्या करना चाहिए ??

उत्तर

13

शायद आपको उस थ्रेड के कारण त्रुटि मिल रही है जिसमें आप गतिविधि के संदर्भ का उपयोग कर रहे हैं।

आपको सामान्य थ्रेड के बजाय AsyncTask का उपयोग करना चाहिए। AsyncTask में एक विधि onPreExecute() और onPostExecute() है जो मुख्य धागे पर निष्पादित कर रहे हैं और एक विधि doInBackground() है जो पृष्ठभूमि पर निष्पादित करेगी ताकि आप आसानी से लंबी लाइव प्रक्रिया को कार्यान्वित कर सकें।

आप उल्लेख कर सकते हैं this example

4

यूआई धागा

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

कभी कभी पर निर्दिष्ट किया गया कार्य आप भी इस समस्या का सामना कर सकते चलाता है जब अपमानजनक कोड (जब एकता का उपयोग कर गतिविधि के अंदर है मेरे मामले में के रूप में खेल यंत्र)।

निम्नलिखित कोड मेरे लिए काम किया है।

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

आप एक AsyncTask का उपयोग कर रहे हैं और आप doInBackground धागा में चल रही प्रक्रियाओं, आप इस तरह टोस्ट के रूप में कार्य प्रदर्शन नहीं कर सकते हैं और, उदाहरण के लिए कर रहे हैं एक नया टुकड़ा खोलने है।

जब आपको यह विशेष त्रुटि आती है, तो इस कोड का उपयोग करने के लिए अपना कोड चलाने के लिए एक नया थ्रेड बनाने का प्रयास करें। यह पृष्ठभूमि प्रक्रिया के बजाय इसे अग्रभूमि में लाएगा (जहां तक ​​मैं समझता हूं)।

टुकड़े के लिए:

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

गतिविधि के लिए:

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

दोनों के बीच अंतर केवल कि क्या आप पर अपने धागा चलाने के लिए thisgetActivity() बुला रहे हैं, या है। किसी भी तरह से वे दोनों चल रहे गतिविधि पर एक नया धागा बनाते हैं।

मुझे आशा है कि मदद करता है!

ओह, और एक नोट, AsyncTask में एक तरह के रूप में अपने स्वयं के काम करता है, के रूप में बना सकते हैं:

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

इन कार्यों, जो आपके AsyncTask वर्ग का हिस्सा हैं में, यह वह जगह है जहाँ आप डाल चाहते हैं कोड के टुकड़े runOnUiThread()। उम्मीद है कि यह स्पष्ट है।

संबंधित मुद्दे