cannot resolve getAssets() method

余生长醉 提交于 2019-12-24 19:01:38

问题


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


回答1:


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();
            }


来源:https://stackoverflow.com/questions/37761289/cannot-resolve-getassets-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!