Mar*_*arc 6 java xml android dom android-xml
Android-SDK的org.w3c.dom.Node.insertBefore的描述如下:
public abstract Node insertBefore (Node newChild, Node refChild)
Inserts the node newChild before the existing child node refChild. If refChild is null, insert newChild at the end of the list of children.
但是,如果我执行以下操作,则会在insertBefore实现中出现NullPointerException:
if(doc != null && doc.getFirstChild() != null && tmpNode != null)
doc.getFirstChild().insertBefore(tmpNode, null);
Run Code Online (Sandbox Code Playgroud)
WARN/System.err(11029): at org.apache.harmony.xml.dom.InnerNodeImpl.insertBefore(InnerNodeImpl.java:86)
我尝试使用Android 2.2和Android 2.3.3!
对我来说,这似乎是一个错误.任何人都可以确认/复制吗?
// edit(18.01.2012 13:05):
我创建了一个新的java项目,因为我想看看它是否适用于java应用程序:
public static void main(String[] args) {
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
try {
docBuilder = dbfac.newDocumentBuilder();
Document d = docBuilder.newDocument();
if(d != null){
d.appendChild(d.createElement("root"));
if(d.getFirstChild() != null){
d.getFirstChild().insertBefore(d.createElement("foo"), null);
System.out.println(d.getFirstChild().getFirstChild().getNodeName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码完美无缺.
我还创建了一个新的android项目来再次测试它:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
try {
docBuilder = dbfac.newDocumentBuilder();
Document d = docBuilder.newDocument();
if(d != null){
d.appendChild(d.createElement("root"));
if(d.getFirstChild() != null){
d.getFirstChild().insertBefore(d.createElement("foo"), null);
System.out.println(d.getFirstChild().getFirstChild().getNodeName());
}
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
当应用程序到达insertBefore时,抛出上面显示的Exception.
所以相同的代码适用于普通的Java,但不适用于Android.对我来说,似乎它仍然是org.w3c.dom的apache和实现中的一个错误.还有其他想法吗?
| 归档时间: |
|
| 查看次数: |
1193 次 |
| 最近记录: |