0

I need to instantiate some objects in a separate thread cause i don' t want the UI to get slow. Using AsyncTask i faced the problem of a memory issue: the GC won't deallocate the memory.

So i found the solution declaring AsyncTask as a static inner class. I'm new to android developing so i need your help cause i'm having a NullPointerException. Here is my code:

-static variables because of the inner static class-

public class Wash extends ActionBarActivity {
     private static Effetti effect1,effect2,effect3…effect50
private static  Effetti[] effects;
.
.
.

-the static inner class-

private static class TaskL  extends AsyncTask <Effetti[],                Void,Effetti[]> {

  @Override
        protected Effetti[] doInBackground(Effetti[]...   params) { 

          effects = new Effetti[]{

          effects1 = new Effetti(MyApplication.getAppContext(),R.raw.ef1),  
          effect2=new Effetti(MyApplication.getAppContext(),R.raw.ef2),
          effect3 = new Effetti(MyApplication.getAppContext(),R.raw.ef3),
          effect4 = new Effetti(MyApplication.getAppContext(),R.raw.ef4),
.
.
.
            }; 

             return  effects;

      } 



       @Override
        protected void onPostExecute(Effetti[] result) {

             super.onPostExecute(result);


                }               

       }

The "Effetti" class is a class which contains SoundPool methods for play and stop audio files. Also contains constructors with arguments like context and a resid.

I used MyApplication.getAppContext() trick seen in this post:

Static way to get 'Context' on Android? android

Any suggestion? thaks

Community
  • 1
  • 1
Rufio_oifuR
  • 11
  • 1
  • 1

1 Answers1

0

I think that this line MyApplication.getAppContext() is giving you the NullPointerException.

You could try doing this:

private static class TaskL  extends AsyncTask <Effetti[],Void,Effetti[]> {

 Activity activity;

    updateDashboardContent(Activity a){
        activity=a;
    }

@Override
    protected Effetti[] doInBackground(Effetti[]...   params) { 

      effects = new Effetti[]{

      effects1 = new Effetti(activity.getApplicationContext(),R.raw.ef1),  
      effect2=new Effetti(activity.getApplicationContext(),R.raw.ef2),
      effect3 = new Effetti(activity.getApplicationContext(),R.raw.ef3),
      effect4 = new Effetti(activity.getApplicationContext(),R.raw.ef4),
     .
     .
     .
        }; 

         return  effects;

  } 



   @Override
    protected void onPostExecute(Effetti[] result) {

         super.onPostExecute(result);


            }               

   }

and according to which part of your code you call it like this:

new TaskL(getActivity()).execute(...); or new TaskL(this).execute(...);

If the above snippet doesn't work then try passing to the constructor of TaskL an Effetti array (doing what I did with 'activity') with variables effects1,effects2... already instantiated avoiding instantiating them in the AsyncTask...

n1nsa1d00
  • 856
  • 9
  • 23
  • Thanks for your reply but the snippet you suggest doesn't work for me. I got a RunTimeException Caused by: "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference" . I'm trying to use AsyncTask because of its background working mode,if i do what you suggest,instantiating variables outside the doInBackground() method,the UI will slow down. There are 54 raw audio files and i need a background way to instantiate them. Btw thanks a lot – Rufio_oifuR Mar 29 '15 at 14:00
  • @Rufio_oifuR You're welcome. Try posting more code (like where you call TaskL and the logcat) in order to get more help... – n1nsa1d00 Mar 30 '15 at 01:13
  • Instead of using static classes,could be useful a WeakReference? it should be able to let the GC doing its job... – Rufio_oifuR Mar 30 '15 at 11:24
  • i've tryed following these suggestions : http://rdyonline.net/android-bytes-asynctask/ but it won't work.. – Rufio_oifuR Mar 31 '15 at 20:58
  • UPDATE: --- @Override protected final Effetti[] doInBackground(Effetti[]... params) { final Activity a = weakActivity.get(); Context ctx = a.getBaseContext();---- i got no NullPointerEx and the app seems to work but.. i have the same OutOfMemory error i ve been noticed before declaring the task as static(which should be the final solution to this problem!) what can i do now? – Rufio_oifuR Mar 31 '15 at 21:59