gpt4 book ai didi

java - Android - 使用 getSelectedItem 时出现 NullPointer 异常

转载 作者:行者123 更新时间:2023-12-01 12:42:40 25 4
gpt4 key购买 nike

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

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);
}

在这种情况下,如果单击对象“michigan”,我希望打开另一个 Activity 。实际的 ListView 填充有每个元素的名称(字符串)字段。但是,现在我收到错误。

日志猫:

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 到底给出了什么?我应该如何修复我的代码?

最佳答案

试试这个方法..它会起作用

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


}

关于java - Android - 使用 getSelectedItem 时出现 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24963607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com