performItemClick not working after setAdapter - android

[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.

You are passing null as parameter for the perfomItemClick, have you tried:
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);
Hope this helps...
Regards!

I think you have to pass accountListView in parameter to performItemClick instead of null .

[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.

Related

app crashes on android 4.3 [duplicate]

This question already has answers here:
What is a stack trace, and how can I use it to debug my application errors?
(7 answers)
Closed 8 years ago.
I got an issue with an android app I'm trying to develop. When I test it in an emulator with android 4.4.2 (the target sdk version), everything works fine. But when I run the same code in an emulator with the same settings but android 4.3 instead, the app crashes almost immediately with the catlog error below.
Using the android lint in eclipse (right click on project, "Android tools" > "Run lint: check for common error"), I cannot find any code that doesn't correspond with the minSdkVersion (16).
Anyone got suggestions what's going wrong?
Catlog error:
07-24 12:54:14.592: D/AndroidRuntime(1012): Shutting down VM
07-24 12:54:14.592: W/dalvikvm(1012): threadid=1: thread exiting with uncaught exception (group=0x41465700)
07-24 12:54:14.631: E/AndroidRuntime(1012): FATAL EXCEPTION: main
07-24 12:54:14.631: E/AndroidRuntime(1012): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.protime360_basics/com.example.protime360_basics.MainActivity}: java.lang.NullPointerException
07-24 12:54:14.631: E/AndroidRuntime(1012): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-24 12:54:14.631: E/AndroidRuntime(1012): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-24 12:54:14.631: E/AndroidRuntime(1012): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-24 12:54:14.631: E/AndroidRuntime(1012): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-24 12:54:14.631: E/AndroidRuntime(1012): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 12:54:14.631: E/AndroidRuntime(1012): at android.os.Looper.loop(Looper.java:137)
07-24 12:54:14.631: E/AndroidRuntime(1012): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-24 12:54:14.631: E/AndroidRuntime(1012): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 12:54:14.631: E/AndroidRuntime(1012): at java.lang.reflect.Method.invoke(Method.java:525)
07-24 12:54:14.631: E/AndroidRuntime(1012): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-24 12:54:14.631: E/AndroidRuntime(1012): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-24 12:54:14.631: E/AndroidRuntime(1012): at dalvik.system.NativeStart.main(Native Method)
07-24 12:54:14.631: E/AndroidRuntime(1012): Caused by: java.lang.NullPointerException
07-24 12:54:14.631: E/AndroidRuntime(1012): at com.example.protime360_basics.MainActivity.loginPopup(MainActivity.java:167)
07-24 12:54:14.631: E/AndroidRuntime(1012): at com.example.protime360_basics.MainActivity.onCreate(MainActivity.java:61)
07-24 12:54:14.631: E/AndroidRuntime(1012): at android.app.Activity.performCreate(Activity.java:5133)
07-24 12:54:14.631: E/AndroidRuntime(1012): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-24 12:54:14.631: E/AndroidRuntime(1012): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-24 12:54:14.631: E/AndroidRuntime(1012): ... 11 more
I found the problem thanks to #codeMagic and #Chris Stratton. Line 167 in my code was
if(!username.equals(null)){
I changed that to
if(username != null && !username.isEmpty()){
=> Problem solved!

Landscape crashes when paused and Portrait doesnt

Hello all,
I tried to find an answer online for this problem, but i cannot find a way to fix my problem...
So maybe some of you guys know what i can do to solve it.
So i have a game ( surface view and thread that draws on the cavnas and all that gamy stuff ) and everything works fine in portrait mode, but i want my game to be played in landscape mode, so i have changed the mode in the android manifest like so:
android:screenOrientation="landscape"
Which works fine, everything is the same, except when i want to close the activity ( call onPause method ). When i get the error that the app needs to be force closed.
Now some people solved their problem by adding
android:configChanges="orientation|screenSize|keyboard|keyboardHidden|navigation"
To their manifest files, but that doesn't do the trick for me, i still keep getting NullPointerException.
Here is the logcat:
07-24 21:27:12.160: E/AndroidRuntime(5272): FATAL EXCEPTION: main
07-24 21:27:12.160: E/AndroidRuntime(5272): java.lang.RuntimeException: Unable to pause activity {com.example.fishtruck/com.example.fishtruck.Start}: java.lang.NullPointerException
07-24 21:27:12.160: E/AndroidRuntime(5272): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2706)
07-24 21:27:12.160: E/AndroidRuntime(5272): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2662)
07-24 21:27:12.160: E/AndroidRuntime(5272): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2640)
07-24 21:27:12.160: E/AndroidRuntime(5272): at android.app.ActivityThread.access$800(ActivityThread.java:123)
07-24 21:27:12.160: E/AndroidRuntime(5272): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1154)
07-24 21:27:12.160: E/AndroidRuntime(5272): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 21:27:12.160: E/AndroidRuntime(5272): at android.os.Looper.loop(Looper.java:137)
07-24 21:27:12.160: E/AndroidRuntime(5272): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-24 21:27:12.160: E/AndroidRuntime(5272): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 21:27:12.160: E/AndroidRuntime(5272): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 21:27:12.160: E/AndroidRuntime(5272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
07-24 21:27:12.160: E/AndroidRuntime(5272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
07-24 21:27:12.160: E/AndroidRuntime(5272): at dalvik.system.NativeStart.main(Native Method)
07-24 21:27:12.160: E/AndroidRuntime(5272): Caused by: java.lang.NullPointerException
07-24 21:27:12.160: E/AndroidRuntime(5272): at com.example.fishtruck.Start.onPause(Start.java:113)
07-24 21:27:12.160: E/AndroidRuntime(5272): at android.app.Activity.performPause(Activity.java:4563)
07-24 21:27:12.160: E/AndroidRuntime(5272): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1197)
07-24 21:27:12.160: E/AndroidRuntime(5272): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2693)
07-24 21:27:12.160: E/AndroidRuntime(5272): ... 12 more
And (not sure if relevant but what the heck ) here is how i call the onPause method:
view.stopThread();
this.finish(); //this is called in the activity and view = surfaceView
And the stopThread() method is called like so:
if(thread!=null){
thread.interrupt();
thread.setRunning(false);
isStopped = true;
thread = null;
}
What object do you have on row 113 in Start.java? The object you have there is not initiated correctly (it is null) which makes your app crash.

java.lang.RuntimeException: Unable to instantiate activity - Testing Amazon Mobile Ads

I've posted this question on the Amazon Developer Forum and haven't received an answer in 4 days, I've also sent it to Amazon developer support and haven't received an answer in the same time frame, so I'm here, hoping someone can help.
I'm trying to add 'mobile ads' to a new application. I'm working with Eclipse - Helios
In my manifest I have the correct permissions set
I have the Libraries set, I've checked the build path and project properties and things seem fine, The project builds with no issues
In my code
I have inserted the Application Key
I have inserted AdRegistration.enableLogging(true);
I have inserted AdRegistration.enableTesting(true);
When I use 'Run As - Android App' the app crashes with this and select either of my normal testing devices (Kindle Fire or Samsung Galaxy Tab), it builds find and begins to load the apk unto the the device and then this:
07-24 14:30:40.722: E/AndroidRuntime(2904): FATAL EXCEPTION: main
07-24 14:30:40.722: E/AndroidRuntime(2904): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.thecountrylife/com.thecountrylife.MenuActivity}: java.lang.ClassNotFoundException: Didn't find class "com.thecountrylife.MenuActivity" on path: /data/app/com.thecountrylife-2.apk:/system/app/MetricsApi-2037410.apk:/system/app/com.amazon.dp.logger.apk
07-24 14:30:40.722: E/AndroidRuntime(2904): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2142)
07-24 14:30:40.722: E/AndroidRuntime(2904): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
07-24 14:30:40.722: E/AndroidRuntime(2904): at android.app.ActivityThread.access$600(ActivityThread.java:146)
07-24 14:30:40.722: E/AndroidRuntime(2904): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1245)
07-24 14:30:40.722: E/AndroidRuntime(2904): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 14:30:40.722: E/AndroidRuntime(2904): at android.os.Looper.loop(Looper.java:151)
07-24 14:30:40.722: E/AndroidRuntime(2904): at android.app.ActivityThread.main(ActivityThread.java:5152)
07-24 14:30:40.722: E/AndroidRuntime(2904): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 14:30:40.722: E/AndroidRuntime(2904): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 14:30:40.722: E/AndroidRuntime(2904): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-24 14:30:40.722: E/AndroidRuntime(2904): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-24 14:30:40.722: E/AndroidRuntime(2904): at dalvik.system.NativeStart.main(Native Method)
07-24 14:30:40.722: E/AndroidRuntime(2904): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.thecountrylife.MenuActivity" on path: /data/app/com.thecountrylife-2.apk:/system/app/MetricsApi-2037410.apk:/system/app/com.amazon.dp.logger.apk
07-24 14:30:40.722: E/AndroidRuntime(2904): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
07-24 14:30:40.722: E/AndroidRuntime(2904): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-24 14:30:40.722: E/AndroidRuntime(2904): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-24 14:30:40.722: E/AndroidRuntime(2904): at android.app.Instrumentation.newActivity(Instrumentation.java:1055)
07-24 14:30:40.722: E/AndroidRuntime(2904): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2133)
Any help would be greatly appreciated
Rather than continue to wait for Amazon or beat my head over the issue I simply switched over to Google MobAds, so my manifest is now what Google requires. Thanks anyway.

Crash when using Google Play Game Services (Unity3d in Android)

I have a problem. I am developing a game in Unity3d which uses the official Facebook SDK and NerdGPG for Google Play Game Services. However, there is an annoying bug:
WHen I want to see the Achievements or Leaderboards, it opnes the UI, but AFTER I CLOSE the UI, then the game crashes. The Logcat tells the following:
03-05 00:09:54.325: E/AndroidRuntime(10810): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9002, result=0, data=null} to activity {com.radioactivewasp.flyingwasp/com.facebook.unity.FBUnityPlayerActivity}: java.lang.NullPointerException
03-05 00:09:54.325: E/AndroidRuntime(10810): at android.app.ActivityThread.deliverResults(ActivityThread.java:3182)
03-05 00:09:54.325: E/AndroidRuntime(10810): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3225)
03-05 00:09:54.325: E/AndroidRuntime(10810): at android.app.ActivityThread.access$1100(ActivityThread.java:140)
03-05 00:09:54.325: E/AndroidRuntime(10810): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1275)
03-05 00:09:54.325: E/AndroidRuntime(10810): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 00:09:54.325: E/AndroidRuntime(10810): at android.os.Looper.loop(Looper.java:137)
03-05 00:09:54.325: E/AndroidRuntime(10810): at android.app.ActivityThread.main(ActivityThread.java:4898)
03-05 00:09:54.325: E/AndroidRuntime(10810): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 00:09:54.325: E/AndroidRuntime(10810): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 00:09:54.325: E/AndroidRuntime(10810): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-05 00:09:54.325: E/AndroidRuntime(10810): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-05 00:09:54.325: E/AndroidRuntime(10810): at dalvik.system.NativeStart.main(Native Method)
03-05 00:09:54.325: E/AndroidRuntime(10810): Caused by: java.lang.NullPointerException
03-05 00:09:54.325: E/AndroidRuntime(10810): at com.facebook.unity.FBUnityPlayerActivity.onActivityResult(FBUnityPlayerActivity.java:15)
03-05 00:09:54.325: E/AndroidRuntime(10810): at android.app.Activity.dispatchActivityResult(Activity.java:5390)
03-05 00:09:54.325: E/AndroidRuntime(10810): at android.app.ActivityThread.deliverResults(ActivityThread.java:3178)
Anybody know what could be the solution?
It looks like your Facebook plugin Activity doesn't know how to handle the onActivityResult() callback when you use the RC_UNUSED(9002) requestID.So I imagine you would want to modify your Facebook Player activity such that the NerdGPG plugin handles the callback.

Runtime Crash on constructing Gson object with NoClassDefFoundError

I am developing one android application and in that app I am trying to use Gson Library for Json serialization and de-serialization. I downloaded the library from the following link:-
http://code.google.com/p/google-gson/downloads/list
I included the gson-2.2.2.jar in Java Build Path, but the application crashes at run time when constructing Gson object:-
Gson gson = new Gson();
in logcat I get
07-24 14:53:21.648: E/dalvikvm(488): Could not find class 'com.google.gson.Gson', referenced from method com.google.gson.examples.android.GsonProguardExampleActivity.onCreate
07-24 14:53:21.648: W/dalvikvm(488): VFY: unable to resolve new-instance 10 (Lcom/google/gson/Gson;) in Lcom/google/gson/examples/android/GsonProguardExampleActivity;
07-24 14:53:21.668: D/dalvikvm(488): VFY: replacing opcode 0x22 at 0x0010
07-24 14:53:21.668: D/dalvikvm(488): VFY: dead code 0x0012-007a in Lcom/google/gson/examples/android/GsonProguardExampleActivity;.onCreate (Landroid/os/Bundle;)V
07-24 14:53:21.788: D/AndroidRuntime(488): Shutting down VM
07-24 14:53:21.788: W/dalvikvm(488): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-24 14:53:21.814: E/AndroidRuntime(488): FATAL EXCEPTION: main
07-24 14:53:21.814: E/AndroidRuntime(488): java.lang.NoClassDefFoundError: com.google.gson.Gson
07-24 14:53:21.814: E/AndroidRuntime(488): at com.google.gson.examples.android.GsonProguardExampleActivity.onCreate(GsonProguardExampleActivity.java:40)
07-24 14:53:21.814: E/AndroidRuntime(488): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-24 14:53:21.814: E/AndroidRuntime(488): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-24 14:53:21.814: E/AndroidRuntime(488): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-24 14:53:21.814: E/AndroidRuntime(488): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-24 14:53:21.814: E/AndroidRuntime(488): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-24 14:53:21.814: E/AndroidRuntime(488): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 14:53:21.814: E/AndroidRuntime(488): at android.os.Looper.loop(Looper.java:123)
07-24 14:53:21.814: E/AndroidRuntime(488): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-24 14:53:21.814: E/AndroidRuntime(488): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 14:53:21.814: E/AndroidRuntime(488): at java.lang.reflect.Method.invoke(Method.java:521)
07-24 14:53:21.814: E/AndroidRuntime(488): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-24 14:53:21.814: E/AndroidRuntime(488): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-24 14:53:21.814: E/AndroidRuntime(488): at dalvik.system.NativeStart.main(Native Method)
Error is:-
07-24 14:53:21.814: E/AndroidRuntime(488): java.lang.NoClassDefFoundError: com.google.gson.Gson
Also, If I include full source of Gson library as another package in my project it all works well.
What am I doing wrong, is this the correct jar??
Seems you have kept the jar file inside lib folder, rather the name should be changed to libs. This was updated newer revisions of ADT (revision 17 onwards).

Categories

Resources