使用HashMap和集合的空指针异常

时间:2013-06-29 23:22:13

标签: collections android-listview nullpointerexception hashmap

我正在尝试按字母顺序对ListView进行排序,而且我很困难。这是我发现的唯一没有直接错误的方法,但是在模拟器上运行时会抛出空指针异常。如果有人有更好的方式做到这一点,我愿意接受提议!

以下是导致问题的我的活动部分:(我不想发布整个活动,它非常大,并没有多大帮助。在我的活动中没有其他与此代码有关的内容,除了

ArrayList<HashMap<String, String>> recipeList;

public void onItemClick(AdapterView<?> parent, View itemClicked,
                int position, long id) {

                String stringName = (String) (lv.getItemAtPosition(position));

        Intent intenttwo = new Intent(SecondScreenActivity.this, ThirdScreenActivity.class);
        intenttwo.putExtra("position", stringName);
        startActivity(intenttwo);

//EDIT:  I added an extra bit here to show that position is a string (name
         of item) rather than an int.

定义recipeList和position)

Collections.sort(recipeList,new Comparator<HashMap<String,String>>(){
    public int compare(HashMap<String,String> mapping1,HashMap<String,String> mapping2){
        return mapping1.get("position").compareTo(mapping2.get("position"));
    };

});

这里是logcat:

06-30 10:46:02.945: E/AndroidRuntime(904): FATAL EXCEPTION: main
06-30 10:46:02.945: E/AndroidRuntime(904): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.SecondScreenActivity}: java.lang.NullPointerException
06-30 10:46:02.945: E/AndroidRuntime(904):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-30 10:46:02.945: E/AndroidRuntime(904):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-30 10:46:02.945: E/AndroidRuntime(904):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-30 10:46:02.945: E/AndroidRuntime(904):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-30 10:46:02.945: E/AndroidRuntime(904):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 10:46:02.945: E/AndroidRuntime(904):  at android.os.Looper.loop(Looper.java:137)
06-30 10:46:02.945: E/AndroidRuntime(904):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-30 10:46:02.945: E/AndroidRuntime(904):  at java.lang.reflect.Method.invokeNative(Native Method)
06-30 10:46:02.945: E/AndroidRuntime(904):  at java.lang.reflect.Method.invoke(Method.java:511)
06-30 10:46:02.945: E/AndroidRuntime(904):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-30 10:46:02.945: E/AndroidRuntime(904):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-30 10:46:02.945: E/AndroidRuntime(904):  at dalvik.system.NativeStart.main(Native Method)
06-30 10:46:02.945: E/AndroidRuntime(904): Caused by: java.lang.NullPointerException
06-30 10:46:02.945: E/AndroidRuntime(904):  at java.util.Collections.sort(Collections.java:1890)
06-30 10:46:02.945: E/AndroidRuntime(904):  at com.example.myapp.SecondScreenActivity.onCreate(SecondScreenActivity.java:91)
06-30 10:46:02.945: E/AndroidRuntime(904):  at android.app.Activity.performCreate(Activity.java:5104)
06-30 10:46:02.945: E/AndroidRuntime(904):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-30 10:46:02.945: E/AndroidRuntime(904):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

另外,我没有任何接近2180线的地方,所以我不知道它来自哪里。 非常感谢能帮助的人

1 个答案:

答案 0 :(得分:0)

我对Android开发知之甚少,但看起来你正在使用“位置”作为关键。你想使用int变量位置作为地图的关键吗?所以你不应该使用:

mapping1.get(位置+ “”)的compareTo(mapping2.get(位置+。 “”));

mapping1.get(Integer.toString(位置))的compareTo(mapping2.get(Integer.toString(位置)));