こんにちは私は値を解析してリストビューで表示するAndroidアプリケーションを使用しています。XML解析メソッドを使用してデータを解析し、リストビューに正常に表示しています。ここで問題となるのは、値(NULL)を持たないxmlタグを解析しようとすると、エラーが発生し、アプリが強制的に閉じられることです。ノードに何らかの値がある場合は、解析のみを許可し、それでも問題が発生し、最初のリスト項目に最初のタグのみが表示されるようにします。そして他のリスト項目は何も表示されません。
Below is the xml tags to parse.
<UserScore>
<GameId>daily_1349375400</GameId>
<Location></Location>
<GameName>Football - 5th October 2013</GameName>
<Date>Oct 5, 2013</Date>
</UserScore>
−
<UserScore>
<GameId>136</GameId>
<GameName>Company Cricket</GameName>
<Location>France </Location>
<Date>Jan 6, 2012</Date>
</UserScore>
For the first time the location tag having "france" as its value, but when again played the second location tag contains nothing.
Here is my coding please tell me the solution.
public class AsynTaskScoreboard extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog = ProgressDialog.show(getParent(), "Retrieving data ", " Loading. Please wait ... ", true);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
URL xpto = new URL(my url here);
HttpURLConnection conn;
conn = (HttpURLConnection) xpto.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
doc.getDocumentElement().normalize();
NodeList nodeList;
nodeList = doc.getElementsByTagName("TotalPoints");
Log.d("Background", "TotalPoints " + nodeList);
for (int i = 0; i < nodeList.getLength(); i++) {
Node name = nodeList.item(i);
NodeList nodeEle = name.getChildNodes();
strTotalPoints = ((Node) nodeEle.item(0)).getNodeValue();
Log.d("Background", "TotalPoints " + nodeList);
}
nodeList = doc.getElementsByTagName("ChallengesPlayed");
for (int i = 0; i < nodeList.getLength(); i++) {
Node name = nodeList.item(i);
NodeList nodeEle = name.getChildNodes();
strChallengesPoint = ((Node) nodeEle.item(0)).getNodeValue();
}
android.os.Message msg = Message.obtain();
msg.what = 11;
handler.sendMessage(msg);
/**
* Check whether it is my points or high score
*/
switch (flagMypointsOrHighScore) {
case 0:
nodeList = doc.getElementsByTagName("UserScore");
// Define Array based on nodelist
arrLength = nodeList.getLength();
strGameid = new String[arrLength];
strGameNameArr = new String[arrLength];
strGameLocationArr = new String[arrLength];
strGameDate = new String[arrLength];
String[] strXMLTag = { "GameId", "GameName", "Location", "Date" };
for (int i = 0; i < arrLength; i++) {
Node node = nodeList.item(i);
Log.d("scoreList", "node " + node);
Element fstElmnt = (Element) node;
Log.d("scoreList", "fstElmnt " + fstElmnt);
for (int j = 0; j < strXMLTag.length; j++) {
NodeList scoreList = fstElmnt.getElementsByTagName(strXMLTag[j]);
Element scoreElement = (Element) scoreList.item(0);
scoreList = scoreElement.getChildNodes();
switch (j) {
case 0:
strGameid[i] = ((Node) scoreList.item(0)).getNodeValue();
break;
case 1:
strGameNameArr[i] = ((Node) scoreList.item(0)).getNodeValue();
break;
case 2:
strGameLocationArr[i]=((Node) scoreList.item(0)).getNodeValue();
break;
case 3:
strGameDate[i] = ((Node) scoreList.item(0)).getNodeValue();
break;
}
}
}// End of for loop
// Your rank
break;
case 1:// Top rank 5
nodeList = doc.getElementsByTagName("Top5Rank");
// Define Array based on nodelist
arrLength = nodeList.getLength();
strGameNameArr = new String[arrLength];
strGameLocationArr = new String[arrLength];
strGameDate = new String[arrLength];
for (int i = 0; i < arrLength; i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList rankList = fstElmnt.getElementsByTagName("Person");
//
int rankListLength = rankList.getLength();
if (strPersonNameArr == null) {
strPersonNameArr = new String[rankListLength];
strPersonRankArr = new String[rankListLength];
strPersoonPointsArr = new String[rankListLength];
}
for (int j = 0; j < rankListLength; j++) {
rankList = fstElmnt.getElementsByTagName("Person");
Element rankElement = (Element) rankList.item(j);
rankList = rankElement.getChildNodes();
strPersonNameArr[j] = rankElement.getAttribute("Name");
strPersonRankArr[j] = rankElement.getAttribute("Rank");
strPersoonPointsArr[j] = rankElement.getAttribute("Points");
}
}// End of for loop
break;
}// End of switch
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}// try below db 2try
} catch (Exception e) {
// TODO Auto-generated catch block
if (e.toString().equalsIgnoreCase("java.net.SocketException: Network unreachable")) {
android.os.Message msg = android.os.Message.obtain();
msg.what = 0;
handler.sendMessage(msg);
}
e.printStackTrace();
}
return null;
}// End of do in background method
// do the stuff after the heavy task
protected void onPostExecute(Void result) {
android.os.Message msg = Message.obtain();
switch (flagMypointsOrHighScore) {
case 0:
listViewMyPoints.setAdapter(new JR_LB_ListAdapter(ScoreboardActivity.this));
break;
case 1:
listViewHighScores.setAdapter(new JR_LB_ListAdapter_HighScores(ScoreboardActivity.this));
// Set textview value in the
msg.what = 10;
handler.sendMessage(msg);
break;
default:
}
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}// End of on post execute method
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
Totalpoint.setText(strTotalPoints);
challengeplay.setText(strChallengesPoint);
txtViewRank.setText(strRank);
txtViewPoint.setText(strPoint);
txtViewPersonName.setText(strPersonName);
Log.d("ProgressUpdate", "strTotalPoints " + strTotalPoints);
Log.d("ProgressUpdate", "strChallengesPoint " + strChallengesPoint);
Log.d("ProgressUpdate", "strRank " + strRank);
Log.d("ProgressUpdate", "strPoint " + strPoint);
}
}// End of Asynchronous task class
asynctaskの後、インテントを介して値を次のアクティビティに渡しました。その時点で、値もnullとして渡されます。その値を使用して、さらにアクティビティを進めます。この問題を解決する方法を教えてください。