[解决了]
我的代码中有以下结构:
ListView accountListView = (ListView) findViewById(R.id.accountListing);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_view_item, R.id.nameTextView, currentAccounts);
accountListView.setAdapter(adapter);
accountListView.performItemClick(null, 0, 0);
currentAccounts 是一个数组,上面有数据。
当我在设置适配器后尝试 performItemClick 时,我得到一个 NullPointerException。
我还覆盖了 OnKeyPress,运行“performItemClick”,它工作正常。
我想知道为什么在设置适配器后尝试执行单击时它不起作用。(经过一些调试,我看到 lastVisiblePosition 是-1)为什么?
[编辑]
使用((ListView)findViewById(R.id.accountListing)).performItemClick(null, 0, 0);
工作,除了,如果在设置适配器后立即使用。(查看评论以获取更多信息)
[编辑 2]
堆栈跟踪:
07-24 13:07:56.710: E/AndroidRuntime(9455): FATAL EXCEPTION: main
07-24 13:07:56.710: E/AndroidRuntime(9455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.device/com.app.device.WelcomeActivity}: java.lang.NullPointerException
07-24 13:07:56.710: E/AndroidRuntime(9455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-24 13:07:56.710: E/AndroidRuntime(9455): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
07-24 13:07:56.710: E/AndroidRuntime(9455): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-24 13:07:56.710: E/AndroidRuntime(9455): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
07-24 13:07:56.710: E/AndroidRuntime(9455): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 13:07:56.710: E/AndroidRuntime(9455): at android.os.Looper.loop(Looper.java:137)
07-24 13:07:56.710: E/AndroidRuntime(9455): at android.app.ActivityThread.main(ActivityThread.java:4895)
07-24 13:07:56.710: E/AndroidRuntime(9455): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 13:07:56.710: E/AndroidRuntime(9455): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 13:07:56.710: E/AndroidRuntime(9455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
07-24 13:07:56.710: E/AndroidRuntime(9455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
07-24 13:07:56.710: E/AndroidRuntime(9455): at dalvik.system.NativeStart.main(Native Method)
07-24 13:07:56.710: E/AndroidRuntime(9455): Caused by: java.lang.NullPointerException
07-24 13:07:56.710: E/AndroidRuntime(9455): at com.app.device.WelcomeActivity.unselectViews(WelcomeActivity.java:1637)
07-24 13:07:56.710: E/AndroidRuntime(9455): at com.app.device.WelcomeActivity.access$24(WelcomeActivity.java:1632)
07-24 13:07:56.710: E/AndroidRuntime(9455): at com.app.device.WelcomeActivity$27.onItemClick(WelcomeActivity.java:1447)
07-24 13:07:56.710: E/AndroidRuntime(9455): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-24 13:07:56.710: E/AndroidRuntime(9455): at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
07-24 13:07:56.710: E/AndroidRuntime(9455): at com.app.device.WelcomeActivity.UpdateAccountList(WelcomeActivity.java:1653)
07-24 13:07:56.710: E/AndroidRuntime(9455): at com.app.device.WelcomeActivity.onCreate(WelcomeActivity.java:238)
07-24 13:07:56.710: E/AndroidRuntime(9455): at android.app.Activity.performCreate(Activity.java:5163)
07-24 13:07:56.710: E/AndroidRuntime(9455): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-24 13:07:56.710: E/AndroidRuntime(9455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
07-24 13:07:56.710: E/AndroidRuntime(9455): ... 11 more
提前致谢!
[解决方案]
我试图在该onCreate()
方法上执行ItemClick,但屏幕对用户并不完全可见。使用onWindowFocusChanged()
工作!Android 文档上的 onWindowFocusChanged()。
谢谢大家。