我正在创建一个 android 应用程序,它从位于主活动类的资源文件夹中的 xml 文件中解析数据,然后我将这些数据存储到数组中并尝试将这些数据发送到一个数据类,其中解析数据的数组是分配了一个对象数组。我得到空指针,但没有错误。我是新手,任何帮助将不胜感激。
Here is my MainActivity class:
public class MainActivity extends Activity {
private String[] names;
private String[] positions;
private String[] abouts;
private String[] links;
private String[] images;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Parse data from file
xmlParsed();
final PlayerData team = new PlayerData();
ListView list =(ListView)findViewById(R.id.MainList);
// Binding resources Array to ListAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.label,team.playerNames());
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, ImageActivity.class);
Player play = team.getPlayer(position);
Bundle bundle1 = new Bundle();
bundle1.putSerializable("player",play);
intent.putExtras(bundle1);
startActivityForResult(intent, RESULT_OK);
}
});
}
public void xmlParsed(){
InputStream is = getResources().openRawResource(R.raw.players);
//String file = "res/raw/players.xml";
//InputStream is = this.getClass().getClassLoader().getResourceAsStream(file);
DocumentBuilder docBuilder=null;
try {
docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}catch (ParserConfigurationException e1) {
e1.printStackTrace();
}
try {
Document document = docBuilder.parse(is);
//names
NodeList nameList = document.getElementsByTagName("name");
names= new String[nameList.getLength()];
for(int i=0; i<nameList.getLength();i++)
{
names[i]= nameList.item(i).getFirstChild().getNodeValue();
}
//positions
NodeList positionList = document.getElementsByTagName("position");
positions= new String[positionList.getLength()];
for(int i=0; i<positionList.getLength();i++)
{
positions[i]= positionList.item(i).getFirstChild().getNodeValue();
}
//images
NodeList imageList = document.getElementsByTagName("image");
images= new String [imageList.getLength()];
for(int i=0; i<imageList.getLength();i++)
{
images[i]= imageList.item(i).getFirstChild().getNodeValue();
}
//abouts
NodeList aboutList = document.getElementsByTagName("about");
abouts= new String[aboutList.getLength()];
for(int i=0; i<aboutList.getLength();i++)
{
abouts[i]= aboutList.item(i).getFirstChild().getNodeValue();
}
//links
NodeList linkList = document.getElementsByTagName("about");
links= new String[linkList.getLength()];
for(int i=0; i<linkList.getLength();i++)
{
links[i]= linkList.item(i).getFirstChild().getNodeValue();
}
}catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
} catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
}
}
{
public String[] getNames() { return names; }
public void setNames(String[] names) {this.names=names;}
public String[] getPosition(){return positions;}
public String[] getPositions() {return positions;}
public void setPositions(String[] positions) {this.positions = positions;}
public String[] getAbouts() {return abouts;}
public void setAbouts(String[] abouts) {this.abouts = abouts;}
public String[] getLinks() {return links;}
public void setLinks(String[] links) {this.links = links;}
public String[] getImages() {return images;}
public void setImages(String[] images) {this.images = images;}
//public String getElement(int location) { return images[location]; }
//public void setElement(int value, int location) { imageIds[location] = value;}
}
这是我的数据类:
public class PlayerData extends Player {
private static final long serialVersionUID = 1L;
MainActivity pullXml =new MainActivity();
String[] namesArray= pullXml.getNames();
String[] positionsArray= pullXml.getPositions();
String[] aboutsArray=pullXml.getAbouts();
String[] linksArray=pullXml.getLinks();
String[] imagesArray=pullXml.getImages();
Player[]team= {
new Player(namesArray[0],positionsArray[0],aboutsArray[0],linksArray[0],0),
new Player(namesArray[1],positionsArray[1],aboutsArray[1],linksArray[1],1),
new Player(namesArray[2],positionsArray[1],aboutsArray[1],linksArray[1],1),
new Player(namesArray[3],positionsArray[1],aboutsArray[1],linksArray[1],1),
new Player(namesArray[4],positionsArray[1],aboutsArray[1],linksArray[1],1),
new Player(namesArray[5],positionsArray[1],aboutsArray[1],linksArray[1],1),
new Player(namesArray[6],positionsArray[1],aboutsArray[1],linksArray[1],1),
new Player(namesArray[7],positionsArray[1],aboutsArray[1],linksArray[1],1),
new Player(namesArray[8],positionsArray[1],aboutsArray[1],linksArray[1],1),
};
String [] playerNames(){
String [] names = new String[team.length];
for(int i=0;i<team.length;i++){
names[i]=team[i].getPlayerName();
}
return names;
}
String playerName(int i){return team[i].getPlayerName();}
String playerPosition(int i){return team[i].getPlayerPosition();}
int playerPic(int i){return team[i].getPlayerImage();}
String playerInfo(int i){return team[i].getPlayerInfo();}
String playerURL(int i){return team[i].getPlayerURL();}
Player getPlayer(int i){return team[i];}
}