Illegal access exception with LocalStorage on Android Gingerbread
Asked Answered
I

3

18

I made an Android/Phonegap app, and it is running fine on all devices with several android OS versions, except on some Gingerbread (2.3) phones. I am using Phonegap 1.9.

Debugging the code, I realized an illegal access exception on Javascript, every time I use the HTML5 localStorage.getItem() and localStorage.setItem() methods.

How can I enable the localStorage features on those Android devices?

Incrassate answered 11/10, 2012 at 18:48 Comment(0)
I
8

The illegal exception was caused by JSON.parse() methods when the .getItem() returns null. Just be careful to validate it. Android 2.3, HTML5 localStorage and Phonegap are working perfectly now.

Incrassate answered 20/11, 2012 at 18:31 Comment(0)
P
0

Check the code for your WebView's WebSettings, i.e check the following are called:

WebSettings settings = webView.getSettings();

settings.setJavaScriptEnabled(true);

settings.setDatabaseEnabled(true);

settings.setDatabasePath(this.getApplicationContext()
        .getDir("database", Context.MODE_PRIVATE).getPath());

settings.setDomStorageEnabled(true);
Pirtle answered 16/10, 2012 at 14:10 Comment(2)
I had a hard time by trying to figure out where those settings are. Under phonegap, we need to call super.appView.getSettings() for accessing them. However, is necessary to call super.init() before do it. Otherwise, appView comes null. Let me try by changing those values as you said.Incrassate
Tested already. All those settings are true, but I am not able to store/retrieve anything still.Incrassate
P
0

This validation work for me. :) (javascript in android 2.3 use phonegap)

Storage.prototype.getArray = function(key) {
        if (this.getItem(key)) {
            return JSON.parse(this.getItem(key)) 
        } else {
            //console.log("no error null value");
        }

    }
Picaresque answered 3/10, 2014 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.