LS_
LS_

Reputation: 7129

error with GetApplicationContext()

I'm trying to create some classes in order to have a cleaner code but I've occurred in this error: I have the following class:

import java.io.IOException;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.metaio.sdk.MetaioDebug;
import com.metaio.tools.io.AssetsManager;



public class AssetsExtracter extends AsyncTask<Integer, Integer, Boolean>
{

    @Override
    protected Boolean doInBackground(Integer... params) 
    {
        try 
        {
            AssetsManager.extractAllAssets(getApplicationContext(), true);
        } 
        catch (IOException e) 
        {
            MetaioDebug.printStackTrace(Log.ERROR, e);
            return false;
        }    
        return true;
    }    
}

In my main activity I call the AssetsExtracter class like this:

    // extract all the assets
    AssetsExtracter mTask = new AssetsExtracter();
    mTask.execute(0);  

But I get an error on GetApplicationContext() on my AssetsExtracter class. It should be something related to the context but how should I edit the class in order to fix this?

Upvotes: 0

Views: 228

Answers (4)

Arpit Patel
Arpit Patel

Reputation: 1571

public class AssetsExtracter extends AsyncTask<Integer, Integer, Boolean>
{

    private Context context;
    public void AssetsExtracter(Context context){
         this.context = context;
    }

    @Override
    protected Boolean doInBackground(Integer... params) 
    {
        try 
        {
            AssetsManager.extractAllAssets(context, true);
        } 
        catch (IOException e) 
        {
            MetaioDebug.printStackTrace(Log.ERROR, e);
            return false;
        }    
        return true;
    }    
}

Upvotes: 1

Simon Mokhele
Simon Mokhele

Reputation: 3993

Change your code to be like this. Pass context in your constructor

 public class AssetsExtracter extends AsyncTask<Integer, Integer, Boolean>{
 Context context;

public AssetsExtracter(Context context){
    this.context = context;
} 

@Override
protected Boolean doInBackground(Integer... params) 
{
    try 
    {
        AssetsManager.extractAllAssets(context, true);
    } 
    catch (IOException e) 
    {
        MetaioDebug.printStackTrace(Log.ERROR, e);
        return false;
    }    
    return true;
}    

}

Upvotes: 1

Alexander
Alexander

Reputation: 48252

You are extending AsyncTask.

AsyncTask does not make sense without an Activity or a Service to where she reports onPostExecute().

So, make your class constructor accept your Activity's Context and then use Context.getApplicationContext()

Alternatively if you just need some async execution with no relation to Android UI you can use plain Java Executor.execute()

Upvotes: 1

laalto
laalto

Reputation: 152817

Add a constructor to your AssetsExtracter class that takes a Context as a parameter, store it in a member variable and use the variable in doInBackground().

Likely the async task used to be a non-static inner class in a class that defines getApplicationContext(), such as an Activity. As a self-standing class there's no outer class that would provide the method.

Upvotes: 1

Related Questions