performItem在setAdapter之后无法正常工作

时间:2013-07-23 20:29:48

标签: android click

[解决]

我的代码中有以下结构:

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是一个数组,它上面有数据。

当我在设置适配器后尝试执行ifClick时,我得到一个NullPointerException。

我也覆盖了OnKeyPress,运行“performItemClick”,它运行正常。

我想知道为什么在设置适配器后尝试执行单击时它不起作用。 (经过一些调试后我发现lastVisiblePosition是-1)为什么?

[编辑]

如果在设置适配器后立即使用((ListView)findViewById(R.id.accountListing)).performItemClick(null, 0, 0);,则使用EXCEPT。 (查看评论以获取更多信息)

[编辑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()方法上执行ifClick,但屏幕对用户来说并不完全可见。使用onWindowFocusChanged()工作了! onWindowFocusChanged() on Android Documentation

谢谢大家。

3 个答案:

答案 0 :(得分:0)

您正在传递null作为perfomItemClick的参数,您是否尝试过:

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(adapter, 0, 0);

希望这会有所帮助......

问候!

答案 1 :(得分:0)

我认为你必须将参数中的accountListView传递给performItemClick而不是null。

答案 2 :(得分:0)

[解决]

我试图在onCreate()方法上执行ifClick,但屏幕对用户来说并不完全可见。使用onWindowFocusChanged()工作了! onWindowFocusChanged() on Android Documentation

谢谢大家。