Error Opening APK on Android

Runtime error …\documents\corona projects\EndofEarth\main.lua:52: attempt to index local ‘bg’ (a nil value)

This is the error that apears when I open the apk on Android device. In the simulator it works fine but when I build the apk and install it on the Android device, it gets me the error.

Any help?

display.setStatusBar( display.HiddenStatusBar ) local physics = require "physics" physics.start() physics.setGravity( 0, 0 ) -- Variables local centerX = display.contentCenterX local centerY = display.contentCenterY local xmin = display.screenOriginX - 50 local xmax = display.contentWidth + 50 local ymin = display.screenOriginY - 50 local ymax = display.contentHeight + 50 local spawnIncreaseTimer local spawnNumber=0 local speedBump = 0 local gametimer local nSeconds = 0 local nMins = 0 local nHours = 0 local numSec = 0 local nSecs = 0 local timervalue = 0 local spawnTimer local firebullet local i = 0 local highscore = 0 local restart local blast = audio.loadSound("media/blast.wav") local gameover = audio.loadSound("media/gameover.wav") local backgroundMusic = audio.loadStream("media/backgroundmusic.wav") local backgroundMusicChannel = audio.play( backgroundMusic, { channel=1, loops=-1, fadein=5000 } ) wall1 = display.newLine(display.screenOriginX, display.screenOriginY, display.screenOriginX, display.contentHeight) wall1.isVisible = false wall1.class = "wall" wall2 = display.newLine(display.contentWidth, display.screenOriginY, display.contentWidth, display.contentHeight) wall2.isVisible = false wall2.class = "wall" wall3 = display.newLine(display.screenOriginX, display.contentHeight, display.contentWidth, display.contentHeight) wall3.isVisible = false wall3.class = "wall" wall4 = display.newLine(display.screenOriginX, display.screenOriginY, display.contentWidth, display.screenOriginY) wall4.isVisible = false wall4.class = "wall" -- Functions local bg = display.newImage("images/starbg.png") bg.x = centerX bg.y = centerY local logo = display.newImage("images/Logo.png") logo.x = centerX logo.y = centerY local begin = display.newImage("images/start.png") begin.x = centerX + 50 begin.y = centerY + 45 -- Esta função aumenta, de 15 em 15 segundos, o numero de asteroides que aparecem por cada 2 segundos local function spawnIncrease() spawnNumber=spawnNumber+1 spawnIncreaseTimer = timer.performWithDelay( 15000, spawnIncrease) end -- Esta função adiciona o score ao display local function startGame() scoreText = display.newText( "Score", 0, 0, "Helvetica", 22 ) scoreText2 = display.newText("0", 0, 0, "Helvetica", 22) scoreText.x = display.screenOriginX + 50 scoreText.y = display.screenOriginY + 20 scoreText2.x = display.screenOriginX + 50 scoreText2.y = display.screenOriginY + 45 end -- Esta função adiciona o timer ao display local function timerGame() timerTxt = display.newText("Time", 0, 0, "Helvetica", 22) timerTxt2 = display.newText("00:00:00", 0, 0, "Helvetica", 22) timerTxt.x = display.contentWidth - 15 timerTxt.y = display.screenOriginY + 20 timerTxt2.x = display.contentWidth - 15 timerTxt2.y = display.screenOriginY + 45 end -- Esta função dá inicio ao timer local function timeCount() nSeconds = timervalue if nSeconds == 0 then timerTxt2.text = "00:00:00"; else nHours = string.format("%02.f", math.floor(nSeconds/3600)); nMins = string.format("%02.f", math.floor(nSeconds/60 - (nHours\*60))); nSecs = string.format("%02.f", math.floor(nSeconds - nHours\*3600 - nMins \*60)); timerTxt2.text = nHours..":"..nMins..":"..nSecs end timervalue = timervalue + 1 gametimer = timer.performWithDelay( 1000, timeCount) end -- Esta função faz parar o aumento de asteroides que aparecem local function stopSpawnIncrease() timer.cancel( spawnIncreaseTimer ) end -- Esta função dá inicio ao jogo local function go(event) Runtime:removeEventListener("tap", go) if (losttxt ~= nil) then display.remove(losttxt) end display.remove(logo) display.remove(begin) local planet = display.newImage("images/terra.png") planet.x = centerX planet.y = centerY planet:scale(4, 4) transition.to( planet, { time = 200, xScale = 1, yScale = 1} ) local asteroidgroup = display.newGroup() startGame() timerGame() timeCount() spawnIncrease() local function firebullet(event) local bullet = display.newCircle( planet.x, planet.y, 2 ) bullet.class = "bullet" physics.addBody( bullet,"dynamic", { density=3.0, friction=0.5, bounce=0.05 } ) bullet.isBullet = true bullet.type = "bullet" local deltax = event.x - planet.x local deltay = event.y - planet.y local bulletangle = math.atan2(deltay, deltax) local xforce = (math.cos(bulletangle))\*40 local yforce = (math.sin(bulletangle))\*40 bullet:applyForce( xforce, yforce, bullet.x, bullet.y ) end Runtime:addEventListener ( "tap", firebullet) local lost -- Esta função faz com que os asteroides apareçam local function spawn() for i=1,spawnNumber do local allAsteroids = {"images/enemy1.1.png" , "images/enemy2.1.png"} asteroids = display.newImage(allAsteroids[math.random(#allAsteroids)]) asteroids.x = math.random(xmin, xmax) asteroids.y = math.random(ymin, ymax) asteroidgroup:insert(asteroids) local pentagonShape = { 0,-13, 13,-5, 13,13, -13,13, -13,-5 } asteroids.class = "asteroid" physics.addBody(asteroids, "static", {shape = pentagonShape, density=1.0, friction=0.3, bounce=0.2}) asteroids.isSensor = true asteroids.type = "asteroids" if (asteroids.x \> display.screenOriginX and asteroids.x \< display.contentWidth + 1 and asteroids.y \> display.screenOriginY and asteroids.y \< display.contentHeight + 1) then display.remove(asteroids) else transition.to( asteroids, { time=math.random(3000-speedBump , 5000-speedBump), y = centerY, x = centerX , rotation = math.random(50 , 180), onComplete = lost}) if (speedBump \< 2000) then speedBump = speedBump + 15 end end end spawnTimer = timer.performWithDelay( 2000, spawn) if (spawnNumber == 5) then stopSpawnIncrease() end end timer.performWithDelay(5000, spawn) function lost() Runtime:removeEventListener ("tap", firebullet) timer.cancel(spawnTimer) display.remove(planet) transition.cancel() --display.remove(asteroids) --asteroids = nil display.remove(asteroidgroup) asteroidgroup = nil display.remove(scoreText) display.remove(scoreText2) timer.cancel(gametimer) display.remove(timerTxt) display.remove(timerTxt2) audio.play(gameover) local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory ) local file, errorString = io.open( path, "r" ) if not file then print( "File error: " .. errorString ) else local highscore = file:read( "\*a" ) io.close( file ) losttxt = display.newText( "You Lose", 0, 0, "Helvetica", 30) losttxt.x = centerX losttxt.y = 40 finalscore = display.newText( "Final Score", 0, 0, "Helvetica", 22 ) finalscore.x = centerX + 150 finalscore.y = 90 finalscore2 = display.newText( "0", 0, 0, "Helvetica", 20) finalscore2.x = centerX + 150 finalscore2.y = 115 finalscore2.text = normalscore highscore2 = display.newText( "High Score", 0, 0, "Helvetica", 22 ) highscore2.x = centerX - 150 highscore2.y = 90 timeplayed = display.newText("Time Played", 0, 0, "Helvetica", 22) timeplayed.x = centerX timeplayed.y = 140 timeplayed2 = display.newText(nHours..":"..nMins..":"..nSecs, 0, 0, "Helvetica", 20) timeplayed2.x = centerX timeplayed2.y = 165 retry = display.newText("Retry", 0, 0, "Helvetica", 30) retry.x = centerX retry.y = display.contentHeight - 50 local highscore4 = display.newText("0", display.screenOriginX - 100, display.screenOriginY - 100, "Helvetica", 22) highscore4.text = highscore highscore = tonumber(highscore) if (normalscore \>= highscore) then highscore = normalscore else highscore = highscore end highscore4.text = highscore highscore3 = display.newText( highscore , 0, 0, "Helvetica", 20) highscore3.x = centerX - 150 highscore3.y = 115 local saveData = highscore4.text local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory ) local file, errorString = io.open( path, "w" ) if not file then print( "File error: " .. errorString ) else file:write( saveData ) io.close( file ) end end retry:addEventListener("tap", restart) end end Runtime:addEventListener ( "tap", go) local function onGlobalCollision( event ) if ( event.phase == "began" ) then if (event.object1.type == "bullet" and event.object2.class == "asteroid") or (event.object2.type == "bullet" and event.object1.class == "asteroid") then local myAsteroid if event.object1.type == "bullet" then myAsteroid = event.object2 myBullet = event.object1 transition.cancel(event.object2) else myAsteroid = event.object1 myBullet = event.object2 transition.cancel(event.object1) end audio.play(blast) display.remove(myAsteroid) display.remove(myBullet) normalscore = i + 1 scoreText2.text = i + 1 i = i + 1 end end end Runtime:addEventListener( "collision", onGlobalCollision ) local function onGlobalCollision2( event ) if ( event.phase == "began" ) then if (event.object1.type == "bullet" and event.object2.class == "wall") or (event.object2.type == "bullet" and event.object1.class == "wall") then local myBullet2 if event.object1.type == "bullet" then myBullet2 = event.object1 else myBullet2 = event.object2 end display.remove(myBullet2) end end end Runtime:addEventListener( "collision", onGlobalCollision2 ) restart = function() display.remove(losttxt) display.remove(finalscore) display.remove(finalscore2) display.remove(highscore2) display.remove(highscore3) display.remove(timeplayed) display.remove(timeplayed2) display.remove(retry) spawnNumber = 0 speedBump = 0 nSeconds = 0 nMins = 0 nHours = 0 numSec = 0 nSecs = 0 timervalue = 0 i = 0 normalscore = 0 scoreText2.text = 0 go() end

Android is case sensitive (simulator( just Windows I think) is not) are you sure the actual image is starbg.png

Neither Windows or macOS is case sensitive. The latest public build (2906) and later daily builds issue warnings in the simulator console log window if they detect file-name mis-matches. It should show up as yellow background text.

If you’re not using the latest public build, I highly recommend updating to take advantage of this. Regardless, the device log (adb logcat, monitor or the Corona Simulator console window with your device tethered, if you had the sim install your app) will show you a warning about not finding the file.

Rob

If you’re using image scaling in your config.lua, make sure that you have corresponding versions of your images present (i.e. @2x, @4x). If you’re not sure, you can test this by trying various resolutions, from low to high, in the simulator. If you get the same error, you know that is the issue.

Android is case sensitive (simulator( just Windows I think) is not) are you sure the actual image is starbg.png

Neither Windows or macOS is case sensitive. The latest public build (2906) and later daily builds issue warnings in the simulator console log window if they detect file-name mis-matches. It should show up as yellow background text.

If you’re not using the latest public build, I highly recommend updating to take advantage of this. Regardless, the device log (adb logcat, monitor or the Corona Simulator console window with your device tethered, if you had the sim install your app) will show you a warning about not finding the file.

Rob

If you’re using image scaling in your config.lua, make sure that you have corresponding versions of your images present (i.e. @2x, @4x). If you’re not sure, you can test this by trying various resolutions, from low to high, in the simulator. If you get the same error, you know that is the issue.