Search code examples
androidinputstreamassets

cannot resolve getAssets() method


hi i have been having trouble with the getAsset() method. i trying to get a xml file from the assets folder with the getAsset() to be put into a inputStream.

CODE:

public class MainActivity extends AppCompatActivity {
    List people;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            InputStream is =  getAssets().open("people.xml");
            people = XMLParser.readPeople(is);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

XML:

<people>
   <person>
       <name>joe</name>
       <dob>11/08/16</dob>
       <gender>male</gender>
   </person>
</people>

can anyone tell me what going on with the getAssets() method


Solution

  • Instead of this

          try {
                InputStream is =  getAssets().open("people.xml");
                people = XMLParser.readPeople(is);
            }catch (IOException e){
                e.printStackTrace();
            }
    

    use this

            try {
                   AssetManager assetManager = getBaseContext().getAssets();
                    InputStream is =  assetManager.open("people.xml");
                    people = XMLParser.readPeople(is);
                }catch (IOException e){
                    e.printStackTrace();
                }