Error running on Android device

Dear sirs,

I am getting the following error, when running my app on a Android device, but everything is OK running on simulator, iPad or iPhone.

Could you help me, please ?

Log generated from Android device:

I/Corona ( 3492): Lua Runtime Error: lua_pcall failed with status: 2, error message is: ?:0: attempt to call method ‘insert’ (a nil value)

[import]uid: 62613 topic_id: 30045 reply_id: 330045[/import]

Are your files in sub-directories like separate folders for images sounds and things like that? Android does not seem to be able to use sub-folders at all. Also you should check the spelling for any sprite sheets you are using. For some reason art programs seem to save sprite sheets as .PNG and .png. [import]uid: 49863 topic_id: 30045 reply_id: 120333[/import]

Not true. Android does support subdirectories.

The message “…attempt to call method ‘insert’ (a nil value)” means that you are calling a function that does not exist, or at least from a table/library that does not exist. Perhaps you forgot to [lua]require[/lua] in “sqlite3”?

Please see the following API documentation page for some example code…
http://docs.coronalabs.com/api/library/sqlite3/index.html
[import]uid: 32256 topic_id: 30045 reply_id: 120337[/import]

OK I wasn’t aware that this bug had been fixed. Nice to know. [import]uid: 49863 topic_id: 30045 reply_id: 120342[/import]

Hi guys !

It is important to remember that the same code runs OK on iPad and iPhone.

Because this, I think that I did not forget any requires.

I am not using subdirectories and I am not using sqlite, but I am loading images dinamicaly.

Thank you so much !
Marcos Tito. [import]uid: 62613 topic_id: 30045 reply_id: 120415[/import]

The log is giving you a “nil value” warning. So, perhaps while building your SQL query string, you forgot to wrap your variables with [lua]tostring()[/lua] functions to convert numbers to strings? That is a common mistake. I think iOS allows you to get away with not doing that, but Android requires it.

For example:
[lua]local sqlQuery = "SELECT x FROM my_table WHERE y = " … tostring(y)[/lua]
[import]uid: 32256 topic_id: 30045 reply_id: 120517[/import]

Are your files in sub-directories like separate folders for images sounds and things like that? Android does not seem to be able to use sub-folders at all. Also you should check the spelling for any sprite sheets you are using. For some reason art programs seem to save sprite sheets as .PNG and .png. [import]uid: 49863 topic_id: 30045 reply_id: 120333[/import]

Not true. Android does support subdirectories.

The message “…attempt to call method ‘insert’ (a nil value)” means that you are calling a function that does not exist, or at least from a table/library that does not exist. Perhaps you forgot to [lua]require[/lua] in “sqlite3”?

Please see the following API documentation page for some example code…
http://docs.coronalabs.com/api/library/sqlite3/index.html
[import]uid: 32256 topic_id: 30045 reply_id: 120337[/import]

OK I wasn’t aware that this bug had been fixed. Nice to know. [import]uid: 49863 topic_id: 30045 reply_id: 120342[/import]

Hi guys !

It is important to remember that the same code runs OK on iPad and iPhone.

Because this, I think that I did not forget any requires.

I am not using subdirectories and I am not using sqlite, but I am loading images dinamicaly.

Thank you so much !
Marcos Tito. [import]uid: 62613 topic_id: 30045 reply_id: 120415[/import]

Hi guys,

I found the problem…

I am using system.pathForFile( “data.txt”, system.ResourceDirectory ) as such does not exist on Android. See below.

Does anybody have a suggestion for me ?
Some examples ?

Thank you !!!

http://developer.coronalabs.com/content/system-os

"Note
system.ResourceDirectory, as such, does not exist on Android. Android applications are packaged into an .apk, which is basically a ZIP file, and cannot be read as normal files. Assets fall into the following categories:

Images, which are loaded directly using native Android APIs.
Sounds, also loaded directly using native Android APIs.
Other data files that you want to access from your Corona app.
On iOS, system.pathForFile( “data.txt”, system.ResourceDirectory ) returns a path to a file in the application resource bundle. This is a directory and files in it may be read normally. On Android, however, the application is delivered in a .apk file. It is not possible to use normal file I/O to access its contents. To allow Corona apps to include generic data files on Android, system.pathForFile has an additional behavior. It copies the resource file from the ZIP archive to a private storage directory associated with the application and then returns the path to that file. Note that the storage on an actual device may be limited. You should not use this facility for large files, and if the device runs out of space, then the data copy may fail." [import]uid: 62613 topic_id: 30045 reply_id: 121203[/import]

The log is giving you a “nil value” warning. So, perhaps while building your SQL query string, you forgot to wrap your variables with [lua]tostring()[/lua] functions to convert numbers to strings? That is a common mistake. I think iOS allows you to get away with not doing that, but Android requires it.

For example:
[lua]local sqlQuery = "SELECT x FROM my_table WHERE y = " … tostring(y)[/lua]
[import]uid: 32256 topic_id: 30045 reply_id: 120517[/import]

Correct, files inside an APK cannot be accessed via the io file APIs. This is because the APK is really a zip file, meaning your files are compressed inside of that APK. That said, we do have a “hack” in Corona which will automatically extract files of particular extensions when you call the [lua]system.pathForFile()[/lua] function. For example, your *.txt files will be auto-extracted when you call [lua]system.pathForFile()[/lua] making it readable file file io.

Please read my forum post via the link below. It has a pretty good explanation.
http://developer.coronalabs.com/forum/2011/09/25/application-has-been-corrupted-error#comment-65109
[import]uid: 32256 topic_id: 30045 reply_id: 121366[/import]

Hi Joshua!

Thank you, so much.

My application needs to load dinamically files of type:

*.png
*.aac
*.txt

Is there a way to do that ?

Thank you, again!
Regards.
[import]uid: 62613 topic_id: 30045 reply_id: 121495[/import]

The [lua]system.pathForFile()[/lua] function will auto-extract all file types EXCEPT the following:

  • *.html
  • *.htm
  • *.3gp
  • *.m4v
  • *.mp4
  • *.png
  • *.jpg
  • *.ttf

So, if you have a file ending with one of the above extensions that you want to access via file IO, then you’ll need to change its extension to something that’s not in the above list. For example, change *.png to *.png_x. It’s a hack, but it works.

Your *.txt files will be extracted for you, because they’re not in the above file extension list. You just have to call [lua]system.pathForFile()[/lua] on them first. [import]uid: 32256 topic_id: 30045 reply_id: 121568[/import]

Hi guys,

I found the problem…

I am using system.pathForFile( “data.txt”, system.ResourceDirectory ) as such does not exist on Android. See below.

Does anybody have a suggestion for me ?
Some examples ?

Thank you !!!

http://developer.coronalabs.com/content/system-os

"Note
system.ResourceDirectory, as such, does not exist on Android. Android applications are packaged into an .apk, which is basically a ZIP file, and cannot be read as normal files. Assets fall into the following categories:

Images, which are loaded directly using native Android APIs.
Sounds, also loaded directly using native Android APIs.
Other data files that you want to access from your Corona app.
On iOS, system.pathForFile( “data.txt”, system.ResourceDirectory ) returns a path to a file in the application resource bundle. This is a directory and files in it may be read normally. On Android, however, the application is delivered in a .apk file. It is not possible to use normal file I/O to access its contents. To allow Corona apps to include generic data files on Android, system.pathForFile has an additional behavior. It copies the resource file from the ZIP archive to a private storage directory associated with the application and then returns the path to that file. Note that the storage on an actual device may be limited. You should not use this facility for large files, and if the device runs out of space, then the data copy may fail." [import]uid: 62613 topic_id: 30045 reply_id: 121203[/import]

Correct, files inside an APK cannot be accessed via the io file APIs. This is because the APK is really a zip file, meaning your files are compressed inside of that APK. That said, we do have a “hack” in Corona which will automatically extract files of particular extensions when you call the [lua]system.pathForFile()[/lua] function. For example, your *.txt files will be auto-extracted when you call [lua]system.pathForFile()[/lua] making it readable file file io.

Please read my forum post via the link below. It has a pretty good explanation.
http://developer.coronalabs.com/forum/2011/09/25/application-has-been-corrupted-error#comment-65109
[import]uid: 32256 topic_id: 30045 reply_id: 121366[/import]

Hi Joshua!

Thank you, so much.

My application needs to load dinamically files of type:

*.png
*.aac
*.txt

Is there a way to do that ?

Thank you, again!
Regards.
[import]uid: 62613 topic_id: 30045 reply_id: 121495[/import]

The [lua]system.pathForFile()[/lua] function will auto-extract all file types EXCEPT the following:

  • *.html
  • *.htm
  • *.3gp
  • *.m4v
  • *.mp4
  • *.png
  • *.jpg
  • *.ttf

So, if you have a file ending with one of the above extensions that you want to access via file IO, then you’ll need to change its extension to something that’s not in the above list. For example, change *.png to *.png_x. It’s a hack, but it works.

Your *.txt files will be extracted for you, because they’re not in the above file extension list. You just have to call [lua]system.pathForFile()[/lua] on them first. [import]uid: 32256 topic_id: 30045 reply_id: 121568[/import]

Thank you. [import]uid: 62613 topic_id: 30045 reply_id: 124269[/import]