1
林建设一个Android应用程序,我有一个Web服务器的基于PHP页面返回像安卓:从Java传递HTML字符串的JavaScript
{"categories":[{"id":123,"name":"Category 1","img":""},{"id":12,"name":"Category 2","img":""},{"id":56,"name":"Category 2","img":""}]}
现在我已经设法在Java中使用这些数据,让每一个数据我需要的元素。香港专业教育学院读一个模板文件中有这样的代码:
<a class=button id="category_id"><img src="img_path"></img>category_name</a>
在Java中我对这个模板
content += template.replace("category_id", id).replace("category_name", name).replace("img_path", img_path);
这是一个正在做for循环的每个类别的内容做与string.replace而现在的值的字符串:
<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>
现在这个字符串在javascript像调用时:
function showAndroidCategories() {
var html = Android.showCategories();
document.getElementById('.button-layout').innerHTML = html;
}
其中.button-layout是我的webView上的div。然而 我得到这个错误:
"Uncaught TypeError: Cannot set property 'innerHTML' of null", source: file:///android_asset/www/index.html (32)
这是指我的javascript的最后一行,你可以在上面看到。 所有我试图做的是从数据中加载一个类别列表,并为每个类别创建一个按钮。
谢谢。乔希。
辉煌的地方,我不相信阻止我的代码工作的唯一东西是'。' :/ 我已经接受了你的回答,但我不能投票给你,直到我看起来有更多的声望。 – jburley89
不用担心。很高兴有帮助:) – Ruraj