这是我的代码:
public void onResume() {
super.onResume();
updateSum();
adapter = new SampleAdapter(getActivity());
adapter.add(new SampleItem(agapimena, R.drawable.favorite_empty_gr));
HotOrNot_all entry2 = new HotOrNot_all(getActivity());
entry2.open();
Cursor cursor = entry2.getAllData();
Cursor cursor2 = entry2.getDataCat2();
Map<String, List<String>> catMap = new TreeMap<String, List<String>>();
if (cursor2 == null) {
catMap.put("Ειδήσεις", new LinkedList<String>());
idMap.put("Ειδήσεις", 1);
catMap.get("Ειδήσεις").add(article);
} else {
for (int pos = 0; pos < cursor.getCount(); pos++) {
if (cursor2.moveToFirst()) // data?
do {
catMap.put(cursor2.getString(cursor2
.getColumnIndex("category")),
new LinkedList<String>());
idMap.put(cursor2.getString(cursor2
.getColumnIndex("category")), new Integer(
pos));
catMap.get(
cursor2.getString(cursor2
.getColumnIndex("category"))).add(
article);
} while (cursor2.moveToNext());
}
}
cursor.close();
for (Entry<String, List<String>> entry : catMap.entrySet()) {
if (cursor2 == null) {
adapter.add(new SampleItem("Ειδήσεις",
android.R.drawable.ic_menu_search));
} else {
adapter.add(new SampleItem(entry.getKey(),
android.R.drawable.ic_menu_help));
}
titles = entry.getValue();
// item2.setArticleList(titles);
}
setListAdapter(adapter);
entry2.close();
cursor2.close();
}
当我运行它时,我得到了这个例外:
07-31 20:06:45.535: E/AndroidRuntime(12390): FATAL EXCEPTION: main
07-31 20:06:45.535: E/AndroidRuntime(12390): java.lang.RuntimeException: Unable to resume activity {kostas.menu.olympiakos/kostas.menu.olympiakos.ListActivity}: java.lang.NullPointerException
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.os.Looper.loop(Looper.java:137)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-31 20:06:45.535: E/AndroidRuntime(12390): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 20:06:45.535: E/AndroidRuntime(12390): at java.lang.reflect.Method.invoke(Method.java:511)
07-31 20:06:45.535: E/AndroidRuntime(12390): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-31 20:06:45.535: E/AndroidRuntime(12390): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-31 20:06:45.535: E/AndroidRuntime(12390): at dalvik.system.NativeStart.main(Native Method)
07-31 20:06:45.535: E/AndroidRuntime(12390): Caused by: java.lang.NullPointerException
07-31 20:06:45.535: E/AndroidRuntime(12390): at java.util.TreeMap$1.compare(TreeMap.java:72)
07-31 20:06:45.535: E/AndroidRuntime(12390): at java.util.TreeMap$1.compare(TreeMap.java:70)
07-31 20:06:45.535: E/AndroidRuntime(12390): at java.util.TreeMap.find(TreeMap.java:277)
07-31 20:06:45.535: E/AndroidRuntime(12390): at java.util.TreeMap.putInternal(TreeMap.java:240)
07-31 20:06:45.535: E/AndroidRuntime(12390): at java.util.TreeMap.put(TreeMap.java:186)
07-31 20:06:45.535: E/AndroidRuntime(12390): at kostas.menu.olympiakos.ListActivity$SampleListFragment.onResume(ListActivity.java:258)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1820)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:412)
07-31 20:06:45.535: E/AndroidRuntime(12390): at com.actionbarsherlock.app.SherlockFragmentActivity.onPostResume(SherlockFragmentActivity.java:68)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.app.Activity.performResume(Activity.java:5293)
07-31 20:06:45.535: E/AndroidRuntime(12390): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
07-31 20:06:45.535: E/AndroidRuntime(12390): ... 12 more
如果我改变
Map<String, List<String>> catMap = new TreeMap<String, List<String>>();
到
HashMap<String, List<String>> catMap = new HashMap<String, List<String>>();
该应用程序正常工作。但是我想使用Treemap来从a到z获取数据!
编辑:
这样的东西?
for (int pos = 0; pos < cursor.getCount(); pos++) {
if (cursor2.moveToFirst()) // data?
do {
if(cursor2.getString(cursor2.getColumnIndex("category")).equals("")){
catMap.put("null",
new LinkedList<String>());
idMap.put(cursor2.getString(cursor2
.getColumnIndex("category")), new Integer(
pos));
catMap.get(
cursor2.getString(cursor2
.getColumnIndex("category"))).add(
article);
}
else{
catMap.put(cursor2.getString(cursor2
.getColumnIndex("category")),
new LinkedList<String>());
idMap.put(cursor2.getString(cursor2
.getColumnIndex("category")), new Integer(
pos));
catMap.get(
cursor2.getString(cursor2
.getColumnIndex("category"))).add(
article);
}
} while (cursor2.moveToNext());
}
}
cursor.close();
答案 0 :(得分:0)
TreeMap不接受空键。一个HashMap呢。
一个选项可能是用空字符串替换空键。
答案 1 :(得分:0)
抛出:
NullPointerException - 如果指定的键为null并且此映射使用自然排序,或者其比较器不允许空键
您的null
密钥为put()
。
虽然我怀疑这实际上是问题而且它不应该是null
,但如果你想要TreeMap
允许null
个密钥,那么你必须提供拥有Comparator
,定义/处理了对排序的意义。