Error with storyboard and back button on device

Hi all,

I recently got the back button on my android device working in my app so now it will go back to the menu scene when you press the back button. The problem I’m having is when I go from my menu scene to a new scene, then press the back button (to go back to the menu) and try to go to a new scene, I get an error.

I/Corona  (30026): Runtime error

I/Corona  (30026): ?:0: attempt to call method ‘insert’ (a nil value)

I/Corona  (30026): stack traceback:

I/Corona  (30026):      ?: in function ‘_listener’

I/Corona  (30026):      ?: in function <?:141>

I/Corona  (30026):      ?: in function <?:218>

This is the error copied from the cmd window using adb logcat.

Here is my back button code located in main.lua

local function onKeyEvent(event) local phase = event.phase local keyName = event.keyName print( event.phase, event.keyName ) if ( "back" == keyName and phase == "up" ) then if storyboard.currentScene == "scene\_home" then native.requestExit() return true else storyboard.gotoScene( "scene\_home" ) return true end end return false end Runtime:addEventListener( "key", onKeyEvent )

and here is the code being run when my scene exits

function scene:exitScene( event ) local group = self.view if (timeon == true) then timer.cancel(timerid) timeon = false end if (flingTimeOn == true) then timer.cancel(flingTimer) flingTimeOn = false end Runtime:removeEventListener( "touch", changePic ) display.remove( textGroup ) end

I also have a display object that is a button to go back to the menu scene and it works fine. The only time this error happens is with the back button on my android device.

Any thoughts?

Is there more to the adb logcat?

It sounds like it may be an error in the scene you’re trying to go to.   Time for some print statements to see where you’re getting to.

Rob

Hi Rob,

That is all from the adb logcat. I did use some print statements to see if my code was being executed and it was.

Another thing that might help is that I am using the storyboard group and inserting the display objects into that group but I don’t see why this would’t work. I’ve tried disregarding the storyboard group from

function scene:createScene( event ) local group = self.view --my code here end

and making my own group instead to see it if works but it still gives me the same problem.

I am also creating a table and inserting objects into a table at the beginning of each scene like this

for i=0,30 do table.insert( imgTest, i, "imageName"..i ) end

Would this have anything to do with it?

An example of the problem: When I open “scene_flower” then press back to go to the menu and open “scene_water” I can see in the adb logcat that it is in the “scene_water” and everything is working but it is still showing “scene_flower” on the screen.

If its still showing stuff from an old scene, then you are not inserting things into the group.

Rob

I’m not sure where I should be putting my print statements. Why would it work with the display object menu button but not the device’s back button? It is the same code being executed. The only thing that is different is that my back button code is in my main.lua.

I would put prints in each of your scene’s events indicating if you’re getting there.  Include the scene name in the print too so you can separate the scenes.

I put print statements in each event and ran the adb logcat and its reading each print statement as it should. I’m getting to each part of the scene correctly, too. Perhaps there is a workaround? Or a way I can put my back button code in each scene without it firing multiple times (a problem I had before I moved it to main.lua)?

No, you should only need to put this in main.lua once.  Anything more is going to lead to problems.  With all the print statements in there, what is being printed before you get the insert is nil error?  That should help you pin point where its happening.

Rob

After running the adb logcat again, it prints the following:
 
remove previous scene (before my create scene function)
create scene 5
will enter scene 5
enter scene 5
 
Then I push the back button and it prints the following:
 
back button code here
exit scene 5 (where my code is that removes listeners, etc.)
destroy scene 5
remove previous scene (before my create scene function in the menu screen)
create scene menu
will enter scene menu
enter scene menu
 
Then I try to go back into the same scene 5 and this prints (the same thing happens with other scenes with their respective print statements):
 
create scene 5
will enter scene 5
enter scene 5
Runtime error
?:0: attempt to call method ‘insert’ (a nil value)
stack traceback:
        ?: in function ‘_listener’
        ?: in function (?:141)
 
So it looks like my back button code is being called before my scene exits. In my back button code, there is the command storyboard.gotoScene( “scene_home” )  but in my scene 5 exit scene function, I am removing all of the listeners and such. Perhaps it is not able to remove the listeners before going to the home scene?

What does your enterScene() look like for scene 5?

Currently my enterScene() for scene 5 is empty with the exception of the print statement. I have the majority of my code in my createScene(). I have put print statements in every function I have as well as every place I am inserting something into a group and they are all running fine, too. Is there a way to call a function in scene 5 in my back button code in main.lua? If so, I could perform a check in the back button code to see if it is scene 5 and if it is scene 5 then I could run the menu button function in my scene 5? My menu button in scene 5 works every time. It only gives me this error when I press the back button.

Bump.

Can you post your code for Scene 5?

Hi Rob,

My code is fairly long so I am attaching the .lua file.

Any help is appreciated.

Is there more to the adb logcat?

It sounds like it may be an error in the scene you’re trying to go to.   Time for some print statements to see where you’re getting to.

Rob

Hi Rob,

That is all from the adb logcat. I did use some print statements to see if my code was being executed and it was.

Another thing that might help is that I am using the storyboard group and inserting the display objects into that group but I don’t see why this would’t work. I’ve tried disregarding the storyboard group from

function scene:createScene( event ) local group = self.view --my code here end

and making my own group instead to see it if works but it still gives me the same problem.

I am also creating a table and inserting objects into a table at the beginning of each scene like this

for i=0,30 do table.insert( imgTest, i, "imageName"..i ) end

Would this have anything to do with it?

An example of the problem: When I open “scene_flower” then press back to go to the menu and open “scene_water” I can see in the adb logcat that it is in the “scene_water” and everything is working but it is still showing “scene_flower” on the screen.

If its still showing stuff from an old scene, then you are not inserting things into the group.

Rob

I’m not sure where I should be putting my print statements. Why would it work with the display object menu button but not the device’s back button? It is the same code being executed. The only thing that is different is that my back button code is in my main.lua.

I would put prints in each of your scene’s events indicating if you’re getting there.  Include the scene name in the print too so you can separate the scenes.

I put print statements in each event and ran the adb logcat and its reading each print statement as it should. I’m getting to each part of the scene correctly, too. Perhaps there is a workaround? Or a way I can put my back button code in each scene without it firing multiple times (a problem I had before I moved it to main.lua)?