Android - 使用getSelectedItem的NullPointer异常

时间:2014-07-25 20:01:27

标签: java android listview nullpointerexception onitemclicklistener

我有一个ListView,其中填充了对象的String字段。我在点击每个元素时尝试打开一个新活动。

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String selectedValue = l.getSelectedItem().toString();
    if(selectedValue.equals(michigan.getName())){
        Intent i = new Intent(getApplicationContext(), CollegeInfo.class);
        i.putExtra("tag", "university-of-michigan-ann-arbor");
        startActivity(i);
    }

在这种情况下,如果对象"密歇根"点击,我希望另一个活动是开放的。实际的ListView使用每个元素的名称(String)字段填充。但是,现在我收到了一个错误。

logcat的:

07-25 15:42:29.681: E/AndroidRuntime(322): java.lang.NullPointerException
07-25 15:42:29.681: E/AndroidRuntime(322):  at com.collegeselector.CollegeList.onListItemClick(CollegeList.java:137)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.widget.AbsListView.onTouchEvent(AbsListView.java:3435)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.View.dispatchTouchEvent(View.java:7239)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-25 15:42:29.681: E/AndroidRuntime(322):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
07-25 15:42:29.681: E/AndroidRuntime(322):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
07-25 15:42:29.681: E/AndroidRuntime(322):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.View.dispatchPointerEvent(View.java:7419)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.os.MessageQueue.nativePollOnce(Native Method)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.os.MessageQueue.next(MessageQueue.java:125)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.os.Looper.loop(Looper.java:124)
07-25 15:42:29.681: E/AndroidRuntime(322):  at android.app.ActivityThread.main(ActivityThread.java:5039)
07-25 15:42:29.681: E/AndroidRuntime(322):  at java.lang.reflect.Method.invokeNative(Native Method)
07-25 15:42:29.681: E/AndroidRuntime(322):  at java.lang.reflect.Method.invoke(Method.java:511)
07-25 15:42:29.681: E/AndroidRuntime(322):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-25 15:42:29.681: E/AndroidRuntime(322):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-25 15:42:29.681: E/AndroidRuntime(322):  at dalvik.system.NativeStart.main(Native Method)

我如何正确使用getSelectedItem来做我想要的事情。此外,我无法使用位置变量,因为ListView中元素的顺序并不总是相同。 getSelectedItem究竟给出了什么?我该怎么做才能修复我的代码?

1 个答案:

答案 0 :(得分:0)

尝试这种方式..它会起作用

 protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Object selectedValue = this.getListAdapter().getItem(position);
    String value = selectedValue.toString();
    Toast.makeText(this, "You have chosen  " + value , Toast.LENGTH_LONG).show();


}