我想按诊所名称对列表进行排序,幸运的是解析器工作正常。然而,我使用的分类技术却没有。我是否错误地实施了它?
public class BookScreen extends Activity {
//array
ArrayList<HashMap<String, String>> songsList;
ProgressDialog mProgressDialog;
// All static variables
static final String URL = "https://dl.dropboxusercontent.com/u/42241589/test.xml";
// XML node keys
static final String KEY_CLINIC = "clinic"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_TIME = "time";
static final String KEY_ADDRESS = "address";
//static final String KEY_THUMB_URL = "thumbnail";
ListView list;
LazyAdapter adapter;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_main);
new DownloadXML().execute();
}
private class DownloadXML extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(BookScreen.this);
mProgressDialog.setTitle("List is loading");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
protected Void doInBackground(Void... params) {
songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
try {
NodeList nl = doc.getElementsByTagName(KEY_CLINIC); //define parent node
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_TIME, parser.getValue(e, KEY_TIME));
map.put(KEY_ADDRESS, parser.getValue(e, KEY_ADDRESS));
//map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args) {
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
Collections.sort(songsList, comparator);
adapter = new LazyAdapter(BookScreen.this, songsList);
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
//Close dialog
mProgressDialog.dismiss();
}
}
Comparator<HashMap<String, String>> comparator = new Comparator<HashMap<String, String>>() {
@Override
public int compare(HashMap<String, String> map1, HashMap<String, String> map2) {
return map1.get(KEY_CLINIC).compareTo(map2.get(KEY_CLINIC));
}
};
}
logcat指向`return map1.get(KEY_CLINIC).compareTo(map2.get(KEY_CLINIC));'的NullPointerException在方法比较器。
12-01 11:25:34.730: E/AndroidRuntime(885): FATAL EXCEPTION: main
12-01 11:25:34.730: E/AndroidRuntime(885): Process: com.example.clinicbooker, PID: 885
12-01 11:25:34.730: E/AndroidRuntime(885): java.lang.NullPointerException
12-01 11:25:34.730: E/AndroidRuntime(885): at com.example.clinicbooker.BookScreen$1.compare(BookScreen.java:102)
12-01 11:25:34.730: E/AndroidRuntime(885): at com.example.clinicbooker.BookScreen$1.compare(BookScreen.java:1)
12-01 11:25:34.730: E/AndroidRuntime(885): at java.util.TimSort.countRunAndMakeAscending(TimSort.java:320)
12-01 11:25:34.730: E/AndroidRuntime(885): at java.util.TimSort.sort(TimSort.java:185)
12-01 11:25:34.730: E/AndroidRuntime(885): at java.util.TimSort.sort(TimSort.java:169)
12-01 11:25:34.730: E/AndroidRuntime(885): at java.util.Arrays.sort(Arrays.java:2023)
12-01 11:25:34.730: E/AndroidRuntime(885): at java.util.Collections.sort(Collections.java:1883)
12-01 11:25:34.730: E/AndroidRuntime(885): at com.example.clinicbooker.BookScreen$DownloadXML.onPostExecute(BookScreen.java:91)
12-01 11:25:34.730: E/AndroidRuntime(885): at com.example.clinicbooker.BookScreen$DownloadXML.onPostExecute(BookScreen.java:1)
12-01 11:25:34.730: E/AndroidRuntime(885): at android.os.AsyncTask.finish(AsyncTask.java:632)
12-01 11:25:34.730: E/AndroidRuntime(885): at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-01 11:25:34.730: E/AndroidRuntime(885): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
12-01 11:25:34.730: E/AndroidRuntime(885): at android.os.Handler.dispatchMessage(Handler.java:102)
12-01 11:25:34.730: E/AndroidRuntime(885): at android.os.Looper.loop(Looper.java:137)
12-01 11:25:34.730: E/AndroidRuntime(885): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-01 11:25:34.730: E/AndroidRuntime(885): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 11:25:34.730: E/AndroidRuntime(885): at java.lang.reflect.Method.invoke(Method.java:515)
12-01 11:25:34.730: E/AndroidRuntime(885): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-01 11:25:34.730: E/AndroidRuntime(885): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-01 11:25:34.730: E/AndroidRuntime(885): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您的比较器正在尝试使用KEY_CLINIC键输入一个条目,但是从来没有一个条目“放入”带有该键的地图。