1

Hello i am developing on android 3.2 and have an activity where i use an asynctask to do an sql query on the worker thread and then populate a listview on the UI thread. Somehow sometimes i get

06-20 15:25:00.270: E/StrictMode(6353): class com.abc.activity.ElencoOrdiniActivity; instances=2; limit=1
06-20 15:25:00.270: E/StrictMode(6353): android.os.StrictMode$InstanceCountViolation: class com.abc.activity.ElencoOrdiniActivity; instances=2; limit=1
06-20 15:25:00.270: E/StrictMode(6353):     at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)

is it my fault ? the async task doesn't hold on to the context, but uses MyActivity.this

here is the code

    AsyncTask<Void, Void, List<JSONArray>> async = new AsyncTask<Void, Void, List<JSONArray>>()
    {

        @Override
        protected List<JSONArray> doInBackground(Void... params) {



            MyApp appState = ((MyApp)getApplicationContext());


            cursor_ordini = getHelper().getReadableDatabase().rawQuery("...");
            cursor_ordini.moveToFirst();

            List<JSONArray> ordini = new Vector<JSONArray>();
            if (cursor_ordini.getCount() > 0)
            {
                ....
                return ordini;

            }
            return null;
        }

        @Override
        protected void onPostExecute(List<JSONArray> ordini) {
            super.onPostExecute(ordini);

            MyApp appState = ((MyApp)getApplicationContext());


            ListView listView = (ListView) ElencoOrdiniActivity.this.findViewById(R.id.listView_elenco_ordini);
            listView.setAdapter( new OrdiniAdapter(ElencoOrdiniActivity.this, R.layout.elenco_ordini_instestazione, ordini));
            listView.setOnScrollListener( new EndlessOrdiniScrollListener(ElencoOrdiniActivity.this, appState.getDittaIdSelezionata(), "") );

        }

    };

    async.execute();
max4ever
  • 11,909
  • 13
  • 77
  • 115

1 Answers1

0

I know that this is old post. Just for guys who is looking for solution and explanation to this problem.

In case there is InstanceCountViolation exception it means that there can be real with Activity leak or problem which is related to how detectActivityLeaks check is implemented in Android SDK.

To identify if this is a problem I can recommend the following post: Detecting leaked Activities in Android. If you will see that there are objects holding a reference to this activity which don't related to Android Framework then you have a problem which should be fixed by you.

In case there are no objects holding a reference to this activity which don't related to Android Framework than it means that you encountered with the problem related to how detectActivityLeaks check is implemented. In this case to fix the problem with failed activity without turning off detectActivityLeaks you can simply run System.gc() before starting activity in debug configuration like in the following example:

 if (BuildConfig.DEBUG)
 {         
     System.gc();
 }

 Intent intent = new Intent(context, SomeActivity.class);
 this.startActivity(intent);

More information are available in this answer.

Community
  • 1
  • 1
Maxim Kornilov
  • 5,675
  • 1
  • 24
  • 35