2016-06-11 151 views
1

您好我一直有getAsset()方法的麻煩。我試圖從資產文件夾中獲取一個xml文件,並將getAsset()放入inputStream中。無法解析getAssets()方法

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

你面臨什麼問題?它拋出異常嗎? – KDeogharkar

回答

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

它與getBaseContext()做同樣的事情 –

+0

@mohammadalam嘗試「清理」該項目和「重建」它。 – Ironman

+0

謝謝我,我不得不重建項目 –