1

I have a line in my class that is supposed to make a call to another class. I put a break point on this line and that break point is never reached, instead I get an error saying:

Can't create handler inside thread that has not called Looper.prepare( )

I have placed my code below. I am getting the error on the line where I try to call the AppTimeUsageClass.

  public  void storeAppTimeUsageData(AppTimeUsage stats) {
       //Context context  = this;
        List<AppTimeUsage> items = new ArrayList<>();

        try {

            appTimeUsageDao.insertOrReplace(stats);

        } catch (Exception e) {

            Log.e("Error", "Some exception occurred", e);
            Log.e("APP_TAG", "STACKTRACE");
            Log.e("APP_TAG", Log.getStackTraceString(e));
        }
        String sql = "SELECT * FROM APP_TIME_USAGE ";
        Cursor c = appTimeUsageDao.getDatabase().rawQuery(sql, null);
        int offset = 0;
        int d ;
        int cd ;
        String e = "";
        while (c.moveToNext()) {
            AppTimeUsage atu = new AppTimeUsage(
                    c.getLong(0),
               new Date (),
             d = c.getInt(2),
            e = c.getString(3)
            // break;
            );
            items.add(atu);
           stats = atu ;

        }
        UploadAppTimeUsageActivity  aapTimeClass = new UploadAppTimeUsageActivity();
        aapTimeClass.postAppTimeUsage(stats);
    }

This is the code of the service that calls the "storeAppTimeUsageData" method

   private boolean addToStatistics(String target )
    {

        Context context  = this;
        SQLiteOpenHelper helper = new DaoMaster.DevOpenHelper(getApplicationContext(), Globals.DatabaseName, null);
        DaoMaster master = new DaoMaster(helper.getWritableDatabase());
        daoSession = master.newSession();
        appTimeUsageDao = DeviceInsightApp.getSession(this, true).getAppTimeUsageDao();


        AppTimeUsage appStats;
        boolean changed = false;
        Date now = new Date();
        if(!TextUtils.isEmpty(target))
        {
            if(!target.equals(foreground))
            {
                int i;
                // timeCheckVariable = i ;
                if(foreground != null && split != null)
                {
                    // TODO: calculate time difference from current moment
                    // to the moment when previous foreground process was activated
                    i = packages.indexOf(foreground);
                    timeCheckVariable = i ;
                    long delta = (now.getTime() - split.getTime()) / 1000;
                    Long time = (Long)processList.get(i).get(ProcessList.COLUMN_PROCESS_TIME);
                    if(time != null)
                    {
                        // TODO: add the delta to statistics of 'foreground'
                        time += delta;
                    }
                    else
                    {
                        time = new Long(delta);
                    }

                    processList.get(i).put(ProcessList.COLUMN_PROCESS_TIME, time);
                    int x = time.intValue( );
                    String applicationName = (String)processList.get(i).get(ProcessList.COLUMN_PROCESS_NAME);
                    appStats = new AppTimeUsage(null , new Date(), x ,applicationName);
                **//Call to storeAppTimeUsageData below**
                    new DataUsageRecorder(context).storeAppTimeUsageData(appStats);

                }

                //update count of process activation for new 'target'
                i = packages.indexOf(target);
                Integer count = (Integer)processList.get(i).get(ProcessList.COLUMN_PROCESS_COUNT);
                if(count != null) count++;
                else
                {
                    count = new Integer(1);
                }
                processList.get(i).put(ProcessList.COLUMN_PROCESS_COUNT, count);
                foreground = target;
                split = now;
                changed = true;
            }
        }
        //Long checkTimeNow = (Long)processList.get(timeCheckVariable).get(ProcessList.COLUMN_PROCESS_TIME);
        return changed;
    }
Zidane
  • 1,696
  • 3
  • 21
  • 35
  • 1
    Possible duplicate of [Can't create handler inside thread that has not called Looper.prepare()](http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare) – Harshad Pansuriya Jun 28 '16 at 07:07
  • @Ironman that question deals with the same error meesage however it is specifically related to UI thread and I doubt my error has anything to do with UI – Zidane Jun 28 '16 at 07:10
  • then try `runOnUiThread()` – Sanoop Surendran Jun 28 '16 at 07:11
  • @Sanoop please clarify? and if you could also explain what the error I am getting means – Zidane Jun 28 '16 at 07:17
  • Could you elaborate? Have you created another thread? Are you trying to communicate with the UI thread from Worker Thread? – Raaja SN Jun 28 '16 at 07:28
  • is the `storeAppTimeUsageData()` running `async` ? – Sanoop Surendran Jun 28 '16 at 07:37
  • Guys doesnt UI thread only apply if you are actually doing something on the User Interface? I have added my post to include the service that calls the storeAppTimeUsageData method – Zidane Jun 28 '16 at 08:07

0 Answers0