我的应用程序想要将数据上传到后端。上传功能可用 2 个地方。一种是在放发票后,它需要上传到后端而不影响其他 UI,这意味着如果用户点击按钮需要上传,通过Service
另一个上传功能将向用户显示所有表格,通过.Upload
AsyncTask
现在的问题是,如果我调用相同的上传表方法,那么它就会出现can't create handler inside thread that has not called looper.prepare()
这个错误。
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.<init>(Handler.java:152)
06-14 09:53:52.514: W/System.err(2288): at android.app.Activity.<init>(Activity.java:714)
06-14 09:53:52.514: W/System.err(2288): at xont.ventura.controller.syn.UploadActivity.<init>(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)
我的代码是:
@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() > 0){
for (Map.Entry<Integer, String> 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;
}
这里的问题是调用new UploadActivity().functionname
如果我在同一个类中编写这个函数。它没有任何问题。喜欢getInvoiceCancelLine()
。如果我这样写,那么有两个地方必须写上传功能方法。我怎样才能重复使用相同的方法 2 地方。
请帮我解决这个问题?
提前致谢。