我正在尝试按字母顺序对我的 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和位置)
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 线附近的任何地方,所以我不确定它来自哪里。非常感谢任何可以提供帮助的人