0

Im building an android app, i have a web server based php page which returns data like

{"categories":[{"id":123,"name":"Category 1","img":""},{"id":12,"name":"Category 2","img":""},{"id":56,"name":"Category 2","img":""}]}

Now i have managed to use this data in java and get each element i need. Ive read a template file in that has code like:

<a class=button id="category_id"><img src="img_path"></img>category_name</a>

In Java i am doing string.replace on the contents of this template

content += template.replace("category_id", id).replace("category_name", name).replace("img_path", img_path);

This is being done in a for loop for each category and is now a string of value:

<a class=button id="123"><img src=""></img>Category 1</a><a class=button id="12"><img src=""></img>Category 2</a><a class=button id="56"><img src=""></img>Category 3</a>

Now this string when called in javascript like:

function showAndroidCategories() {
            var html = Android.showCategories();
            document.getElementById('.button-layout').innerHTML = html;
        }

where .button-layout is a div on my webView. however i get this error:

"Uncaught TypeError: Cannot set property 'innerHTML' of null", source: file:///android_asset/www/index.html (32)

which refers to the last line of my javascript you can see above. All im trying to do is load a list of categories from the data and create a button for each one.

Thanks. Josh.

1 Answer 1

2

You are using button-layout as a class name (.) rather than an ID (#).

function showAndroidCategories() {
     var html = Android.showCategories();
     document.getElementById('button-layout').innerHTML = html;
}

Perhaps this is what you meant to write.

Sign up to request clarification or add additional context in comments.

2 Comments

brilliant spot, i cant believe that the only thing stopping my code from working was '.' :/ I have accepted your answer but i cant vote you up until i have more reputation it seems.
Don't worry about it. Glad to have helped :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.