Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get AssetManager from a library project

I am trying to get AssetManager form a class in an Android library project, but I am getting the error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

This is how I call it:

public class RevPluginLoader extends AppCompatActivity {

    private List<String> copyPlugins() {
        AssetManager assetManager = getAssets();  // This is where it all fails
    }
}

How can I get the AssetManager?

like image 610
Program-Me-Rev Avatar asked Sep 06 '25 03:09

Program-Me-Rev


2 Answers

We can't add assets folder in lib module

Library modules cannot include raw assets The tools do not support the use of raw asset files (saved in the assets/ directory) in a library module. Any asset resources used by an app must be stored in the assets/ directory of the app module itself.

For More details, you can refer here

like image 66
Muthukrishnan Rajendran Avatar answered Sep 07 '25 23:09

Muthukrishnan Rajendran


I think you have a onCreateView

public class RevPluginLoader extends AppCompatActivity {

    private Context mContext = null; //declare a context here
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext  = this; //assign class object to context
    }

    private List<String> copyPlugins() {
        AssetManager assetManager = mContext.getAssets(); //use context here
    }
}

Check commented lines

This may solve your problem

like image 29
CLIFFORD P Y Avatar answered Sep 07 '25 21:09

CLIFFORD P Y