Activities / java.lang.NullPointerException

Kevin

Well-Known Member
Licensed User
Longtime User
After much work, I am nearing the next version release of my app. While putting it through its paces (i.e. trying to break it!) I stumbled on a somewhat rare and hard to reproduce error.

I believe the problem is stemming from the fact that when UserClosed = True on my main (starting) activity, I am killing it with ExitApplication (despite all of the posts here by Erel and several others saying not to do that). I know it is normal to see a process "running" even after closing it but I just don't like to see that there. :eek:

Trying to reproduce this is difficult but I believe it has something to do with Android recreating a process in my app. Below is how to create the error, but it does not happen every time and it takes many many tries:

1) Start the app
2) Start another activity in the app i.e. StartActivity ("Setup")
3) Press the Home button to essentially pause the app
4) Go back to my app either by holding Home and choosing it or pressing on its shortcut/icon
5) 99% of the time, it resumes where it left off. On a rare occasion, it instead starts the app on the main screen as if it is the first time I ran it.
6) When the app fails to resume where I left off but instead appears to start over, if I then press Back, which calls ExitApplication, I get a NullPointerException error in the last activity I was in when I pressed Home. If I tell it to continue anyway it goes through a long series of NPE errors from just about every activity and sub in my app!

I suspect that ExitApplication is my problem, but since it is difficult to test, I was hoping to hear from someone that can tell me (if this is the cause), why is this happening and only occasionally? For all I know there is something else wrong in my app but other than this goofy string of events that leads up to it, it seems solid.

If I do need to remove ExitApplication, then I have another dilemma. I use the network library to detect WiFi. It is declared in Process_Globals:

B4X:
Dim MyLan As ServerSocket

Then initialized in Activity_Create (FirstTime = True):

B4X:
MyLan.Initialize(0, "") ' For getting WiFi status / IP
If MyLan.IsInitialized Then GetNetInfo


I have been closing that socket when the user closes the app (before calling ExitApplication). If I can no longer ExitApplication, where should I close the socket? Or should I open and close before/after I want to check the connection? Maybe open it where I have it in Activity_Create and then close it at the end of the sub GetNetInfo (which is where it determines the IP address of the device)? The reason why I didn't do this initially is because I was afraid it would close the socket before it got a chance to determine the IP address. Should I move the initialize stuff from Activity_Create to Activity_Resume?

There is a lot of good and helpful info here but I'm still a bit off my game in Android land. While I think my app is running pretty smoothly (except for this problem), sometimes I feel like it is nothing more than bits and pieces of disjointed code that may or may not be in the correct spot (Activity_Create as opposed to Resume, FirstTime=True or not, etc). This feeling was made worse by my recent update which allows both orientations for the first time and I had some issues with data loss initially. Reading the forum here helped me get it going but it was more like, "Okay. That didn't work, so maybe I will try this!" It kind of reminds me of way back when I started Windows programming (VB3) and did a lot of experimenting to get things to work right because it was new to me. :D
 

Kevin

Well-Known Member
Licensed User
Longtime User
Well, just to try it, I removed the ExitApplication and tried again. I think I was able to get the app to "restart" after having paused it. I did NOT get an error (a good thing!) but I didn't get desired behavior either.

Now it did this:

1) Start the app
2) Start another activity in the app i.e. StartActivity ("Setup")
3) Press the Home button to essentially pause the app
4) Go back to my app either by holding Home and choosing it or pressing on its shortcut/icon
5) 99% of the time, it resumes where it left off. On a rare occasion, it instead starts the app on the main screen as if it is the first time I ran it.
6) When the app fails to resume where I left off but instead appears to start over, if I then press Back, it brought me back to the screen I was on when I pressed Home. Pressing Back again then "closed" the app and brought me back to the home screen.

No error is good, but how do I keep it from trying to bring me back to the other activity that Android failed to resume in the first place? Or better yet, prevent Android from apparently restarting or recreating my app? :sign0094:

Do I maybe need to save a variable with the state manager containing the last screen (activity) I had open? Then restore it when the app runs? At this point I don't even know if it is truly restarting or if it is some other glitch.
 
Last edited:
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Well, still getting an occasional npe error even after I quit forcing the app to quit. Just when I was about to finally put this version up. I think I made too many large changes in one update. I'm also seeing some strange timer errors in httputils (set to 0, when they shouldn't be, and this is the live version before the major changes. I wonder if it related to losing variable values when android dumps the app on its own.

This might take a while... :(
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Anyone have any thoughts on this? After much testing, reverting to old code, and pulling what little hair remains, I still occasionally get these npe errors. I am tracking some of them down, but others seem to be caused by the os terminating my main activity while using another. Part of the problem is that I read in process global variables from the main activity into a second activity and then attempt to transfer them back to the main activity when done. When I try to do this second transfer, apparently the original variables in the main activity are no longer there because the os kills it.

This is so frustrating since I felt so close to being done when all hell broke loose. :(
 
Upvote 0
Top