嗨,我遇到了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()方法发生了什么
答案 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();
}