【问题标题】:java.lang.ExceptionInInitializerError when creating instance of EHCache in android在android中创建EHCache实例时出现java.lang.ExceptionInInitializerError
【发布时间】:2018-01-22 04:44:19
【问题描述】:

我正在尝试在 Android 中使用 ehCache,并收到以下错误

java.lang.ExceptionInInitializerError
    at net.sf.ehcache.EhcacheDefaultClassLoader.getInstance(EhcacheDefaultClassLoader.java:29)
    at net.sf.ehcache.config.Configuration.<init>(Configuration.java:208)
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:152)
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:103)
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:140)
    at net.sf.ehcache.CacheManager.newInstance(CacheManager.java:892)
    at net.sf.ehcache.CacheManager.create(CacheManager.java:873)
    at net.sf.ehcache.CacheManager.getInstance(CacheManager.java:907)

Caused by: java.lang.NullPointerException: parentLoader == null && !nullAllowed
    at java.lang.ClassLoader.<init>(ClassLoader.java:210)
    at java.lang.ClassLoader.<init>(ClassLoader.java:202)
    at net.sf.ehcache.EhcacheDefaultClassLoader.<init>(EhcacheDefaultClassLoader.java:35)
    at net.sf.ehcache.EhcacheDefaultClassLoader.<clinit>(EhcacheDefaultClassLoader.java:26)
    at net.sf.ehcache.EhcacheDefaultClassLoader.getInstance(EhcacheDefaultClassLoader.java:29) 
    at net.sf.ehcache.config.Configuration.<init>(Configuration.java:208) 
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:152) 
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:103) 
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:140) 
    at net.sf.ehcache.CacheManager.newInstance(CacheManager.java:892) 
    at net.sf.ehcache.CacheManager.create(CacheManager.java:873) 
    at net.sf.ehcache.CacheManager.getInstance(CacheManager.java:907)

这是我尝试初始化的代码

private static Cache getCache(String cacheName) throws IllegalStateException{
    CacheManager cacheManager = CacheManager.getInstance();
    Cache cache;
    if(!cacheManager.cacheExists(cacheName)) {
        cacheManager.addCache(cacheName);
    }
    cache = cacheManager.getCache(cacheName);
    return cache;
}

EHCache 似乎无法在 android 中运行?任何人都可以对此有所了解吗? 收到错误后,我还在 res/xml/ 中放置了一个带有一些配置的 ehcache.xml ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"  monitoring="autodetect" dynamicConfig="true">

    <!-- By default, Ehcache stored the cached files in temp folder. -->    <!-- <diskStore path="java.io.tmpdir" /> -->

    <!-- Ask Ehcache to store cache in this path -->    <!--<diskStore path="c:\\cache" /> -->

    <!-- Sample cache named cache1
    This cache contains a maximum in memory of 10000 elements, and will expire
    an element if it is idle for more than 5 minutes and lives for more than
    10 minutes.

    If there are more than 10000 elements it will overflow to the
    disk cache, which in this configuration will go to wherever java.io.tmp is
    defined on your system. On a standard Linux system this will be /tmp" 
    -->     
    <cache name="cache1" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" transactionalMode="off">         
        <persistence strategy="localTempSwap" />
    </cache>

</ehcache>

【问题讨论】:

  • 请显示您的 ehcache.xml
  • 我已附上xml

标签: java android caching ehcache


【解决方案1】:

Ehcache 无法找到 ehcach.xml 文件。它不知道 res/xml 文件夹。

所以尝试以这种方式以编程方式配置缓存(ehcache 2.10):

// create default CacheManager
Configuration config = new Configuration();
config.setName("Mngmt");
// [...]
CacheManager cacheManager = CacheManager.create(config);

int maxEntriesLocalHeap = 100;
String cachName = "cacheName";
Cache cache = new Cache(
  new CacheConfiguration(cachName, maxEntriesLocalHeap)
    .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
    .eternal(false)
    .timeToLiveSeconds(120)
    .timeToIdleSeconds(60)
    .diskExpiryThreadIntervalSeconds(0));
cacheManager.addCache(cache);

您可以在这里找到更多信息:Creating Caches Programmatically

【讨论】:

  • 谢谢,但我在 cacheManager =CacheManager.getInstance();这个我试过了,没有效果
  • 尝试拨打create而不是getInstance
  • 同样的问题。 getInstance 除了调用 create 之外什么都不做。
猜你喜欢
  • 1970-01-01
  • 2017-12-12
  • 2023-03-20
  • 2016-08-20
  • 2012-07-12
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多