[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. UsingonWindowFocusChanged()
worked! onWindowFocusChanged() on Android Documentation.Thanks everybody.
I think you have to pass accountListView in parameter to performItemClick instead of null .
You are passing null as parameter for the perfomItemClick, have you tried:
Hope this helps...
Regards!