无法解析getAssets()方法

时间:2016-06-11 07:48:37

标签: android inputstream assets

嗨,我遇到了getAsset()方法的问题。我试图从assets文件夹中获取一个xml文件,并将getAsset()放入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>

任何人都可以告诉我getAssets()方法发生了什么

1 个答案:

答案 0 :(得分:2)

而不是这个

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

使用此

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