3

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?

Shreeya Chhatrala
  • 1,441
  • 18
  • 33
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142

2 Answers2

2

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

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
  • I am using assets in lib folder :), including layout – CLIFFORD P Y Aug 11 '17 at 05:59
  • **layout** you can use but asserts you can't as per doc please refer the link which I provided in my answer. If you are using in different way of adding assets in your lib, share us the ideas, so that other people can get help. – Muthukrishnan Rajendran Aug 11 '17 at 06:06
  • on your link "Additionally, an AAR file may include one or more of the following optional entries: assets" . Lol – Alexufo Nov 21 '21 at 10:25
1

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

CLIFFORD P Y
  • 16,974
  • 6
  • 30
  • 45