2017-08-11 35 views
2

我試圖讓AssetManager形式在Android庫項目一類得到AssetManager,但我得到的錯誤:如何從一個庫項目

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

這是我怎麼稱呼它:

public class RevPluginLoader extends AppCompatActivity { 

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

如何獲得AssetManager

+1

你忘了添加上下文嗎? context.getAsset()...., –

+1

您添加資產文件夾的位置..?在你的lib模塊或主模塊中..?嘗試將它添加到主模塊並檢查。 –

+0

我將它添加到lib模塊中@MuthukrishnanRajendran –

回答

1

LIB模塊中,我們不能增加資產的文件夾

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.

更多詳情ls,你可以refer here

+0

我正在使用lib文件夾中的資產:),包括佈局 –

+0

**佈局**您可以使用但斷言你不能按照文檔請參考我提供的鏈接我的答案。如果您以不同的方式在您的lib中添加資源,請與我們分享想法,以便其他人可以獲得幫助。 –

1

我認爲你有一個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 
    } 
} 

檢查註釋行

這可能會解決你的問題

+0

RevPluginLoader除了靜態方法外,還有其他的上下文引用。 –

+0

但錯誤顯示getresourse調用空對象 –

相關問題