AsyncTask不执行的问题

时间:2013-09-17 19:50:46

标签: android android-asynctask

我正在向数据库添加新用户(SQLite数据库)。当我单击按钮创建用户时,我正在使用doInBackground。当我连接到WCF Web服务时它工作正常。我转而使用SQLite数据库,但是没有执行asyncTask的doInBackground方法。所有更改都在AddUser()方法中进行。没有异常被抛出,并且logcat中没有任何异常。我在这里做错了什么?

// Onclick事件代码:

if(!(username.matches("") && password.matches("")&& firstName.matches("") && lastName.matches("")))
            {
                userNameRequired.setVisibility(View.INVISIBLE);
                passwordRequired.setVisibility(View.INVISIBLE);
                firstNameRequired.setVisibility(View.INVISIBLE);
                lastNameRequired.setVisibility(View.INVISIBLE);
              try
              {
                 new Thread(
                     new Runnable() {
                         @Override
                         public void run() {
                  **This is where it stops and does nothing.**
                             new   createNewUserAsyncTask().execute();  
                         }
                     }
             ).start();
                }catch(Exception ex)
                {
                   Log.i("Error message", ex.getMessage());
                }

//这是AsyncTask类

public class createNewUserAsyncTask extends AsyncTask<Void,Void,Boolean>
    {

        @Override
        protected Boolean doInBackground(Void... voids) {
            return AddUser(); //I added a breakpoint here, and it never reached it.
        }
**//Method where the changes were made.**
          private Boolean AddUser()
          {
              try
              {

                  db = dbHelper.getReadableDatabase();
                  ContentValues contentValue = new ContentValues();
                  contentValue.put(dbHelper.PERSONFIRSTNAME, firstName);
                  contentValue.put(dbHelper.PERSONLASTNAME, lastName);
                  contentValue.put(dbHelper.PERSONMIDDLEINITIAL, middleNAme);
                  contentValue.put(dbHelper.PERSONADDRESS1, address);
                  contentValue.put(dbHelper.PERSONCITY, city);
                  contentValue.put(dbHelper.PERSONSTATE, state);
                  contentValue.put(dbHelper.PERSONZIPCODE, zipcode);
                  contentValue.put(dbHelper.PERSONHOMENUMBER, homeNumber);
                  contentValue.put(dbHelper.PERSONCELLNUMBER, cellNumber);
                  contentValue.put(dbHelper.PERSONEMAILADDRESS, emailAddress);
                  contentValue.put(dbHelper.PERSONADDITIONALEMAILADDRESS, additionalEmail);
                  db.insert(dbHelper.PERSON_TABLE, null, contentValue);
                  Cursor cursor = db.rawQuery("SELECT last_insert_rowid() from "+ dbHelper.PERSON_TABLE, null);
                  cursor.moveToFirst();
                  int id = cursor.getInt(0);
                  cursor.close();
                  if(id > 0)
                  {
                      String date = DateCreated();
                       contentValue = new ContentValues();
                      contentValue.put(dbHelper.USERSUSERNAME, username);
                      contentValue.put(dbHelper.USERSPASSWORD, password);
                      contentValue.put(dbHelper.USERSDATECREATED, date);
                      contentValue.put(dbHelper.USERSPERSONID, id);
                      db.insert(dbHelper.USERS_TABLE, null, contentValue);
                      return true;
                  }
              }catch (Exception e)
              {
                  Log.i("Error message: ", e.getMessage());
              }
              return false;
          }

 @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            if(aBoolean == true)
            {
                userIsCreated = aBoolean;
                Intent intent = new Intent(NewUser.this, Login.class);
                startActivity(intent);
            }
        }
    }

这是logcat

  09-17 19:31:17.400    1086-1086/com.mb.progressnfit I/Choreographer: Skipped 3040 frames!  The application may be doing too much work on its main thread.
    09-17 19:31:43.759      639-679/com.android.exchange D/ExchangeService: Received deviceId from Email app: null
    09-17 19:31:43.759      639-679/com.android.exchange D/ExchangeService: !!! deviceId unknown; stopping self and retrying
    09-17 19:31:48.816      639-847/com.android.exchange D/ExchangeService: !!! EAS ExchangeService, onCreate
    09-17 19:31:48.826      639-639/com.android.exchange D/ExchangeService: !!! EAS ExchangeService, onStartCommand, startingUp = false, running = false
    09-17 19:31:48.826      277-450/system_process W/ActivityManager: Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
    09-17 19:31:48.826      639-696/com.android.exchange D/ExchangeService: !!! Email application not found; stopping self
    09-17 19:31:48.836      277-289/system_process W/ActivityManager: Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
    09-17 19:31:48.836      639-639/com.android.exchange D/ExchangeService: !!! EAS ExchangeService, onStartCommand, startingUp = true, running = false
    09-17 19:31:48.856      639-639/com.android.exchange E/ActivityThread: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d1dcc8 that was originally bound here
            android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d1dcc8 that was originally bound here
            at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
            at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
            at android.app.ContextImpl.bindService(ContextImpl.java:1418)
            at android.app.ContextImpl.bindService(ContextImpl.java:1407)
            at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
            at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
            at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
            at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
            at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
            at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
            at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
            at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)
    09-17 19:31:48.987      639-639/com.android.exchange E/StrictMode: null
            android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d1dcc8 that was originally bound here
            at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
            at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
            at android.app.ContextImpl.bindService(ContextImpl.java:1418)
            at android.app.ContextImpl.bindService(ContextImpl.java:1407)
            at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
            at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
            at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
            at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
            at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
            at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
            at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
            at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)
    09-17 19:31:48.987      277-277/system_process W/ActivityManager: Unbind failed: could not find connection for android.os.BinderProxy@40e47718
    09-17 19:31:49.076      639-639/com.android.exchange E/ActivityThread: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d1d920 that was originally bound here
            android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d1d920 that was originally bound here
            at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
            at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
            at android.app.ContextImpl.bindService(ContextImpl.java:1418)
            at android.app.ContextImpl.bindService(ContextImpl.java:1407)
            at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
            at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
            at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
            at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
            at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
            at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
            at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)
    09-17 19:31:49.076      639-639/com.android.exchange E/StrictMode: null
            android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d1d920 that was originally bound here
            at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
            at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
            at android.app.ContextImpl.bindService(ContextImpl.java:1418)
            at android.app.ContextImpl.bindService(ContextImpl.java:1407)
            at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
            at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
            at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
            at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
            at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
            at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
            at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)
    09-17 19:31:49.096      277-465/system_process W/ActivityManager: Unbind failed: could not find connection for android.os.BinderProxy@40ef0ce8
    09-17 19:31:49.096      639-641/com.android.exchange D/dalvikvm: GC_CONCURRENT freed 459K, 20% free 2500K/3104K, paused 80ms+88ms, total 240ms
    09-17 19:32:33.837      639-696/com.android.exchange D/ExchangeService: Received deviceId from Email app: null
    09-17 19:32:33.837      639-696/com.android.exchange D/ExchangeService: !!! deviceId unknown; stopping self and retrying
    09-17 19:32:38.908      639-682/com.android.exchange D/ExchangeService: !!! EAS ExchangeService, onCreate
    09-17 19:32:38.916      639-639/com.android.exchange D/ExchangeService: !!! EAS ExchangeService, onStartCommand, startingUp = false, running = false
    09-17 19:32:38.916      277-462/system_process W/ActivityManager: Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
    09-17 19:32:38.916      639-847/com.android.exchange D/ExchangeService: !!! Email application not found; stopping self
    09-17 19:32:38.926      277-450/system_process W/ActivityManager: Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
    09-17 19:32:38.926      639-639/com.android.exchange D/ExchangeService: !!! EAS ExchangeService, onStartCommand, startingUp = true, running = false
    09-17 19:32:38.946      639-639/com.android.exchange E/ActivityThread: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ceaf88 that was originally bound here
            android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ceaf88 that was originally bound here
            at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
            at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
            at android.app.ContextImpl.bindService(ContextImpl.java:1418)
            at android.app.ContextImpl.bindService(ContextImpl.java:1407)
            at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
            at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
            at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
            at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
            at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
            at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
            at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
            at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)
    09-17 19:32:38.987      639-639/com.android.exchange E/StrictMode: null
            android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ceaf88 that was originally bound here
            at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
            at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
            at android.app.ContextImpl.bindService(ContextImpl.java:1418)
            at android.app.ContextImpl.bindService(ContextImpl.java:1407)
            at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
            at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
            at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
            at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
            at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
            at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
            at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
            at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)

1 个答案:

答案 0 :(得分:2)

更改代码以取出ThreadRunnable

if(!(username.matches("") && password.matches("")&& firstName.matches("") && lastName.matches("")))
        {
            userNameRequired.setVisibility(View.INVISIBLE);
            passwordRequired.setVisibility(View.INVISIBLE);
            firstNameRequired.setVisibility(View.INVISIBLE);
            lastNameRequired.setVisibility(View.INVISIBLE);

              **This is where it stops and does nothing.**
            new   createNewUserAsyncTask().execute();

AsyncTask必须在UI

上执行

According to the Docs 4个线程规则中的一个(实际上是第一个)是

  

必须在UI线程上加载AsyncTask类。这是从JELLY_BEAN开始自动完成的。