-1

这是我的代码:

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();
4

2 回答 2

0

来自TreeMap.put() 的 Javadocs

抛出:
NullPointerException - 如果指定的键为 null 并且此映射使用自然排序,或者它的比较器不允许空键

你的钥匙null在你的put().

虽然我怀疑这实际上是问题所在,而且不应该是null,但如果您想要一个TreeMap允许的null密钥,则必须提供您自己Comparator的定义/处理订购的含义。

于 2013-07-31T17:16:58.027 回答
0

TreeMap 不接受空键。HashMap 可以。

一种选择可能是用空字符串替换空键。

于 2013-07-31T17:12:14.010 回答