问题描述
[已解决]
我的代码中具有以下结构:
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);
可以正常工作,除非在设置适配器后立即使用。 (请参阅评论以获取更多信息)
堆栈跟踪:
07-24 13:07:56.710:E / AndroidRuntime(9455):致命意外:主要
07-24 13:07 :56.710:E / AndroidRuntime(9455):java.lang.RuntimeException:无法启动活动ComponentInfo {com.app.device / com.app.device.WelcomeActivity}:java.lang.NullPointerException
07-24 13 :07:56.710:E / AndroidRuntime(9455):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-24 13:07:56.710:E / AndroidRuntime(9455):在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):位于android.os.Handler.dispatchMessage(Handler.java:99)
07-24 13:07:56.710:E / AndroidRuntime(9455):位于android.os.Looper.loop(Looper.java:137)
07-24 13:07:56.710:E / AndroidRuntime(9455) ):位于android.app.ActivityThread.main(ActivityThread.java:4895)
07-24 13:07:56.710:E / AndroidRuntime(9455):位于java.lang.reflect.Method.invokeNative(本机方法)
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):位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:994)
07-24 13:07:56.710:E / AndroidRuntime(9455):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
07-24 13:07:56.710:E / AndroidRuntime(9455):在dalvik.system.NativeStart.main(本机方法)
07-24 13:07:56.710:E / AndroidRuntime(9455):原因: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):位于com.app.device.WelcomeActivity $ 27.onItemClick(WelcomeActivity.java:1447)
07-24 13:07:56.710:E / AndroidRuntime(9455):位于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 :07:56.710:E / AndroidRuntime(9455):在com.app.device.WelcomeActivity.UpdateAccountList(WelcomeActivity.java:1653)
07-24 13:07:56.710:E / AndroidRuntime(9455):在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):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
07-24 13:07:56.710:E / AndroidRuntime(9455):.. 。另外11个
谢谢!
[解决方案]
我试图在 onCreate()
方法上执行ItemClick,但屏幕不完全对用户可见。使用 onWindowFocusChanged()
有效! 。。 p>
谢谢大家。
[已解决]
我试图在 onCreate()
方法上执行ItemClick,但用户无法完全看到屏幕。使用 onWindowFocusChanged()
有效! 。。 p>
谢谢大家。
[SOLVED]
I have the following structure in my code:
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 is an array, and it has data on it.
When I try to performItemClick after setting the adapter, I get a NullPointerException.
I also override the OnKeyPress, to run the "performItemClick" and it works fine.
I would like to know WHY it doesn't work when trying to perform the click after setting the adapter. (after some debugging I saw that the lastVisiblePosition is -1) WHY?
[EDIT]
Using ((ListView)findViewById(R.id.accountListing)).performItemClick(null, 0, 0);
works, EXCEPT, if used right after setting the adapter. (look at the comments for more info)
[EDIT 2]
Stack Trace:
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
Thanks in advance!
[SOLUTION]
I was trying to performItemClick on the onCreate()
method, but the screen is not fully visible to the user. Using onWindowFocusChanged()
worked! onWindowFocusChanged() on Android Documentation.
Thanks everybody.
[SOLVED]
I was trying to performItemClick on the onCreate()
method, but the screen is not fully visible to the user. Using onWindowFocusChanged()
worked! onWindowFocusChanged() on Android Documentation.
Thanks everybody.
这篇关于setItem之后,performItemClick不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!