Can't create handler inside thread that has not called Looper.prepare() - background Service
<p>My application want to upload data to backend. Upload functionality available 2 places. One is after put invoice, it need to upload to backend without afftect other UI, that means through <code>Service</code>another upload functionality will show all table to user if the user click on <code>Upload</code> button need to upload, through <code>AsyncTask</code>. </p>

<p>Now the problem is, if I call same upload table method then it thow <code>can't create handler inside thread that has not called looper.prepare()</code> this error.</p>

<pre><code>06-14 09:53:52.504: W/System.err(2288): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-14 09:53:52.514: W/System.err(2288):     at android.os.Handler.&lt;init&gt;(Handler.java:152)
06-14 09:53:52.514: W/System.err(2288):     at android.app.Activity.&lt;init&gt;(Activity.java:714)
06-14 09:53:52.514: W/System.err(2288):     at xont.ventura.controller.syn.UploadActivity.&lt;init&gt;(UploadActivity.java:43)
06-14 09:53:52.514: W/System.err(2288):     at xont.ventura.controller.service.UploadService.uploadUsingService(UploadService.java:102)
06-14 09:53:52.514: W/System.err(2288):     at xont.ventura.controller.service.UploadService$ServiceThread.run(UploadService.java:3872)
</code></pre>

<p>My code is :</p>

<pre><code>  @Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "Upload Service Started", Toast.LENGTH_LONG).show();
    if(! APPURL.equals("")){
        serviceThread = new ServiceThread();
        serviceThread.start();
        if(result.equals("The operation timed out")) {
            Toast.makeText(this,"Service is not working  OR Time out to Connect to service !!!!!!!",Toast.LENGTH_LONG).show();
        }
    }else{
        Toast.makeText(this, "GPRS/WIFI is not available", Toast.LENGTH_LONG).show();
    }
    Log.d(TAG, "onStart");
}

    private class ServiceThread extends Thread {
    @Override
    public void run() {
         result = uploadUsingService();
    }   
};


    public String uploadUsingService(){
    uploadTable = getUploadTable();
    String result = "";
    try {
      //  dbAdapter.openDataBase();
        if(uploadTable.size() &gt; 0){
            for (Map.Entry&lt;Integer, String&gt; entry : uploadTable.entrySet()) {
                String value = entry.getValue();
                if(value.equals("WMInvoiceHeader")){
                    result = new UploadActivity().getInvoiceHeader();
                }else if(value.equals("WMInvoiceLine")){
                    result = new UploadActivity().getInvoiceLine();
                }else if(value.equals("WMInvoiceHeaderDiscount")){
                    result = new UploadActivity().getInvoiceHeaderDiscount();
                }else if(value.equals("WMInvoiceSpecialDiscount")){
                    result = new UploadActivity().getInvoiceSpecialDiscount();
                }else if(value.equals("WMInvoiceCancelHeader")){
                    result = new UploadActivity().getInvoiceCancelHeader();
                }else if(value.equals("WMInvoiceCancelLine")){
                    result = new UploadActivity().getInvoiceCancelLine();
                }else if(value.equals("WMTransactionControl")){
                    result = new UploadActivity().getTransactionControl();
                }else if(value.equals("WMVisitDetail")){
                    result = new UploadActivity().getVisitDetail();
                }
                if(result.equals("The operation timed out")) break;
            }
        }
        //Toast.makeText(this, "Upload Service End", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}
</code></pre>

<p>Here the problem is calling <code>new UploadActivity().functionname</code> If i write this function inside the same class. it doesn't have any issue. like <code>getInvoiceCancelLine()</code> .If i write like this then 2 places have to write upload functionality method. How can i reusable same method 2 place.</p>

<p>Please help me out from this issuse?</p>

<p>Thanks in advance. </p>
