在Android中使用Apache Commons lib时的奇怪行为

时间:2011-11-02 03:39:17

标签: android apache-commons

我正在使用commons-lang3-3.0.1.jar,以便使用StrSubstitutor类。看看这个......

从我的SyncAdapter调用以下代码,它崩溃了构建StrSubstitutor:

public static String makeGetPlansQueryString(Bundle params)
{
    Map<String, String> valuesMap = new HashMap<String, String>();

    valuesMap.put(REQUEST_PARAM_APIUSERNAME, API_USERNAME);
    valuesMap.put(REQUEST_PARAM_APIPASSWORD, API_PASSWORD);
    //... etc. etc.

    String xmlTemplate = VendApplication.getContext().getString(R.string.XMLREQUEST_GET_PLANS);
    StrSubstitutor subst = new StrSubstitutor(valuesMap); // <-- Crashes in here.
    return subst.replace(xmlTemplate);
}

错误输出:

    11-02 14:36:08.439: ERROR/AndroidRuntime(9244): FATAL EXCEPTION: SyncAdapterThread-1
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244): java.lang.ExceptionInInitializerError
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244):     at com.conducthq.android.vend.webapi.WebAPIRequestHelper.makeGetPlansQueryString(WebAPIRequestHelper.java:68)
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244):     at com.conducthq.android.vend.webapi.WebAPIClient.createHTTPRequest(WebAPIClient.java:121)
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244):     at com.conducthq.android.vend.webapi.WebAPIClient.processRequest(WebAPIClient.java:55)
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244):     at com.conducthq.android.vend.webapi.SyncAdapter.onPerformSync(SyncAdapter.java:39)
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244):     at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:163)
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244): Caused by: java.lang.ExceptionInInitializerError
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244):     at org.apache.commons.lang3.text.StrMatcher.stringMatcher(StrMatcher.java:206)
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244):     at org.apache.commons.lang3.text.StrSubstitutor.<clinit>(StrSubstitutor.java:112)
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244):     ... 5 more
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244): Caused by: java.lang.NullPointerException
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244):     at org.apache.commons.lang3.StringUtils.<clinit>(StringUtils.java:717)
    11-02 14:36:08.439: ERROR/AndroidRuntime(9244):     ... 7 more

如果我将此一行添加到我的HomeActivity(在应用启动时启动),则上述代码可以完美运行:

StrSubstitutor sub = new StrSubstitutor(new HashMap<String, String>());

奇怪吧?该行如何影响在不同时间点在不同线程上运行的不同代码?任何Android专家都可以对此有所了解吗?是否有一些我不知道的奇怪的Android JAR加载错误? @ _ @

我在LG-P350上使用Android 2.1 SDK。我使用StrSubstitutor上的Apache Commons API文档中的超级简单示例代码重现了这种行为:http://commons.apache.org/lang/api-3.0.1/org/apache/commons/lang3/text/StrSubstitutor.html

1 个答案:

答案 0 :(得分:1)

您的崩溃是在StringUtils的静态初始化程序中,它尝试从当前线程获取上下文类加载器 - 显然是为了获取java.text.Normalizer $ Form类。所以是的,getContextClassLoader()返回null是一个时髦的问题。

更新

this bug中明显提到错误,所以我猜你可以强行设置上下文类加载器,你应该没问题。

Thread.currentThread().setContextClassLoader(this.getClassLoader());