Android的XSL转换空指针,但工作在桌面上 [英] Android xsl transformation null pointer, but working on desktop

查看:98
本文介绍了Android的XSL转换空指针,但工作在桌面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作XSLT在Android上。 Users.xml文件:

I was working on xslt on android. The users.xml file:

<?xml version="1.0" encoding="utf-8"?>
<users>
  <user>
    <fname>somename</fname>
    <hobbies>
      <hobby>Movie</hobby>
      <hobby>Trekking</hobby>
    </hobbies>
  </user>
</users>

该users.xsl文件

The users.xsl file

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <xsl:for-each select="users/user">
                    <h2>
                        <xsl:value-of select="fname" />
                    </h2>
                    <h3>Hobbies :</h3>
                    <xsl:for-each select="hobbies/hobby">
                        <xsl:value-of select="." />
                        <xsl:if test="position() != last()">
                            <xsl:text> , </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Android的布局userview.xml

android layout userview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <WebView
        android:id="@+id/userwebview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

Android的活性

Android activity

public class UserDisplayActivity extends Activity {
    WebView userView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userwebview);
        userView = (WebView) findViewById(R.id.userwebview);
        loadTransformedHtml();
    }
    private void loadTransformedHtml() {
        try {
            String htmlTransformed=UserXmlTransform.getTransformedHtml();
            userView.loadData(htmlTransformed, "text/html", "utf-8");
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
 }

和UserXmlTransform类

And UserXmlTransform class

public class UserXmlTransform {
    static final String sdPath=Environment.getExternalStorageDirectory().getAbsolutePath();
    static final File xmlFileF = new File(sdPath+"/users.xml");
    static final File xsltFileF = new File(sdPath+"/users.xsl");
    public static String getTransformedHtml() throws TransformerException {
        Source srcXml = new StreamSource(xmlFileF);
        Source srcXsl = new StreamSource(xsltFileF);
        StringWriter writer = new StringWriter();
        Result result = new StreamResult(writer);
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(srcXsl);
        transformer.transform(srcXml, result);
        return writer.toString();
    }
}

转型成功与UserXmlTransform.java code运行,而测试的Java项目完全相同的XML和XSL文件。

The transformation is successfully run with UserXmlTransform.java code while testing as java project with exactly same xml and xsl file.

在Android应用程序,该文件是在适当的位置。 但是,在运行空指针异常被抛出的行

In android app, the files are at appropriate location. But while running the NullPointer Exception is thrown at line

transformer.transform(srcXml, result); 

UserXmlTransform.java的。为什么这台变压器的对象成为Android的空。

of UserXmlTransform.java. why this transformer object became null in android.

我想不出有什么问题。请帮助我。 [新增]我使用SDK 2.2

I could not figure out what is problem. Please Help me. [Added] I am using SDK 2.2

推荐答案

我做了如下修改 users.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

和问题解决了。

但仍然不知道为什么这引起了错误。

But still don't know why this caused the error.

这篇关于Android的XSL转换空指针,但工作在桌面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆