我正在构建一个Android应用程序,我有一个基于Web服务器的php页面,它返回像
这样的数据{"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中,我正在对此模板的内容进行string.replace
content += template.replace("category_id", id).replace("category_name", name).replace("img_path", img_path);
这是在每个类别的for循环中完成的,现在是一个值的字符串:
<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的最后一行。 我想要做的就是从数据中加载一个类别列表,并为每个类别创建一个按钮。
感谢。约什
答案 0 :(得分:2)
您使用按钮布局作为班级名称(。)而不是ID(#)。
function showAndroidCategories() {
var html = Android.showCategories();
document.getElementById('button-layout').innerHTML = html;
}
也许这就是你打算写的。