嗨,我正在尝试创建一个 JSONObject,其中包含一个包含固定装置的日期数组,我目前在这一行得到一个空指针异常,if(fixturesDict.length() == 0){
有人知道为什么吗?
这是我的代码
public void FillData() throws JSONException{
ListView list = getListView();
list.scrollTo(0, 0);
list.setVerticalFadingEdgeEnabled(false);
list.setTextFilterEnabled(true);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
fixturesView = LayoutInflater.from(getBaseContext()).inflate(R.layout.fixturescell,
null);
//Log.v("MyFix", "fixturesArray = " + fixturesArray);
if(fixturesArray.length() < 1){
TextView emptytext = (TextView) fixturesView.findViewById(R.id.TextView02);
emptytext.setText("No Upcoming Fixtures Available");
}else{
try{
for(int t = 0; t < fixturesArray.length(); t++){
JSONObject matchDateDict = fixturesArray.getJSONObject(t);
matchDate = matchDateDict.getString("matchdate");
if(matchDatesHeadersArray.length() != 0){
int lm = t - 1;
JSONObject lastDateDict = fixturesArray.getJSONObject(lm);
String lastMatchDate = lastDateDict.getString("matchdate");
if(matchDate.equals(lastMatchDate)){
} else {
matchDatesHeadersArray.put(matchDate);
}
} else {
matchDatesHeadersArray.put(matchDate);
}
}
JSONObject fixturesDict = new JSONObject();
JSONArray tempArray = new JSONArray();
JSONArray newTempArray = new JSONArray();
JSONObject tempDict = null;
for(int f = 0; f < fixturesArray.length(); f++){
String matchDateHeaderString = matchDatesHeadersArray.getString(f);
tempDict = fixturesArray.getJSONObject(f);
String currentMatchDate = tempDict.getString("matchdate");
Log.v("myFix", "matchDateHeaderString = " + matchDateHeaderString + "currentMatchDate = " + currentMatchDate);
*if(fixturesDict.length() == 0){*
tempArray = null;
} else {
tempArray = fixturesDict.getJSONArray(currentMatchDate);
}
if(!currentMatchDate.equals(matchDateHeaderString)){
// ADD TEXT INTO ARRAY FOR HEADER TITLES
}
newTempArray.put(tempDict);
fixturesDict.put(matchDateHeaderString, newTempArray);
Log.v("myFix", "SEMI fixturesDict = " + fixturesDict);
}
Log.v("myFix", "FULL fixturesDict = " + fixturesDict);
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是我得到的错误
07-18 11:01:48.974: E/AndroidRuntime(26155): java.lang.NullPointerException
07-18 11:01:48.974: E/AndroidRuntime(26155):at MIFixtures.FillData(MIFixtures.java:455)
07-18 11:01:48.974: E/AndroidRuntime(26155): at .MIFixtures$PostTask.onPostExecute(MIFixtures.java:390)
07-18 11:01:48.974: E/AndroidRuntime(26155): at e.MIFixtures$PostTask.onPostExecute(MIFixtures.java:1)
07-18 11:01:48.974: E/AndroidRuntime(26155): at android.os.AsyncTask.finish(AsyncTask.java:417)
07-18 11:01:48.974: E/AndroidRuntime(26155): at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-18 11:01:48.974: E/AndroidRuntime(26155): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-18 11:01:48.974: E/AndroidRuntime(26155): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 11:01:48.974: E/AndroidRuntime(26155): at android.os.Looper.loop(Looper.java:143)
07-18 11:01:48.974: E/AndroidRuntime(26155): at android.app.ActivityThread.main(ActivityThread.java:4263)
07-18 11:01:48.974: E/AndroidRuntime(26155): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 11:01:48.974: E/AndroidRuntime(26155): at java.lang.reflect.Method.invoke(Method.java:507)
07-18 11:01:48.974: E/AndroidRuntime(26155): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-18 11:01:48.974: E/AndroidRuntime(26155): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-18 11:01:48.974: E/AndroidRuntime(26155): at dalvik.system.NativeStart.main(Native Method)
这是我的 json 提要
{
"code":200,
"error":null,
"data":{
"fixtures":[
{
"kickoff":"15:00:00",
"matchdate":"2012-07-28",
"homescore":null,
"awayscore":null,
"attendance":null,
"homepens":null,
"awaypens":null,
"division_id":"5059",
"division":"Testing 1",
"comp":"LGE",
"location":null,
"fixture_note":null,
"hometeam_id":"64930",
"hometeam":"Team 1",
"awayteam_id":"64931",
"awayteam":"Team 2"
}, {
"kickoff":"15:00:00",
"matchdate":"2012-07-28",
"homescore":null,
"awayscore":null,
"attendance":null,
"homepens":null,
"awaypens":null,
"division_id":"5059",
"division":"Testing 1",
"comp":"LGE",
"location":null,