我是Android编程的新手,我正在尝试编写一个解析OWL文件(.owl文件)并显示类和子类的Android应用程序。当用户单击该按钮时,将从Eclipse Project的 assets 文件夹中读取该文件,解析OWL文件并将类的名称打印到控制台上。
但是每当我点击按钮时,模拟器都会崩溃,抛出以下异常:
02-23 16:49:36.430: E/AndroidRuntime(597): FATAL EXCEPTION: main
02-23 16:49:36.430: E/AndroidRuntime(597): java.lang.ExceptionInInitializerError
02-23 16:49:36.430: E/AndroidRuntime(597): at com.Sample.SampleApp.StartingPoint.parseOWLFile(StartingPoint.java:105)
02-23 16:49:36.430: E/AndroidRuntime(597): at com.Sample.SampleApp.StartingPoint$3.onClick(StartingPoint.java:68)
这是我写的Java代码:
public class StartingPoint extends ActionBarActivity {
private static final int REQUEST_PATH = 1;
String currentFile;
EditText et1;
Button browse, ok, exit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText)findViewById(R.id.editText);
browse = (Button) findViewById(R.id.browseButton);
browse.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
getfile(v);
}
});
exit = (Button)findViewById(R.id.ExitButton);
exit.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
});
ok = (Button)findViewById(R.id.OKButton);
ok.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
parseOWLFile();
}
});
}
public void getfile(View view)
{
Intent i1 = new Intent(this, FileChooser.class);
startActivityForResult(i1, REQUEST_PATH);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == REQUEST_PATH)
{
if(resultCode == RESULT_OK)
{
currentFile = data.getStringExtra("GetFileName");
et1.setText(currentFile);
}
}
}
public void parseOWLFile()
{
//String file = et1.getText().toString(); //Uncomment this when using on phone/tablet
String file = "antibiotics.owl";
try
{
//System.out.println("File name is "+file);
//File f = new File(file);
InputStream fis = getAssets().open(file);
OntModel base = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
base.read(fis, null);
String ns = base.getNsPrefixURI("Ontology");
Resource r = base.getResource(ns+" Concept");
OntClass theClass = r.as(OntClass.class);
Individual indiv = base.createIndividual(ns+"IndivTest", theClass);
for (Iterator<Resource> i = indiv.listRDFTypes(true); i.hasNext(); )
System.out.println( indiv.getURI() + " is asserted in class " + i.next() );
OntModel inf = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF, base );
Individual ind = inf.getIndividual( ns + "indivTest" );
for (Iterator<Resource> i = ind.listRDFTypes(true); i.hasNext(); )
System.out.println( ind.getURI() + " is inferred to be in class " + i.next() );
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
我已将AndroJena 0.5版本的jar导入我的Eclipse项目。
以下是我得到的一些例外情况:
02-23 16:49:36.430: E/AndroidRuntime(597): Caused by: java.lang.ExceptionInInitializerError
02-23 16:49:36.430: E/AndroidRuntime(597): at com.hp.hpl.jena.ontology.OntModelSpec.<clinit>(OntModelSpec.java:54)
02-23 16:49:36.430: E/AndroidRuntime(597): Caused by: java.lang.ExceptionInInitializerError
02-23 16:49:36.430: E/AndroidRuntime(597): at com.hp.hpl.jena.ontology.ProfileRegistry.<clinit>(ProfileRegistry.java:48)
02-23 16:49:36.430: E/AndroidRuntime(597): Caused by: java.lang.ExceptionInInitializerError
02-23 16:49:36.430: E/AndroidRuntime(597): at com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(ModelFactory.java:122)
02-23 16:49:36.430: E/AndroidRuntime(597): at com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(ModelFactory.java:116)
这是更新的堆栈跟踪:
02-24 21:29:24.273: E/AndroidRuntime(534): FATAL EXCEPTION: main
02-24 21:29:24.273: E/AndroidRuntime(534): java.lang.ExceptionInInitializerError
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.arp.impl.XMLHandler.<init>(XMLHandler.java:367)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.arp.impl.RDFXMLParser.<init>(RDFXMLParser.java:52)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.arp.impl.RDFXMLParser.create(RDFXMLParser.java:85)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.arp.JenaReader.<init>(JenaReader.java:62)
02-24 21:29:24.273: E/AndroidRuntime(534): at java.lang.Class.newInstanceImpl(Native Method)
02-24 21:29:24.273: E/AndroidRuntime(534): at java.lang.Class.newInstance(Class.java:1319)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.model.impl.RDFReaderFImpl.getReader(RDFReaderFImpl.java:114)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:220)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntDocumentManager.findMetadata(OntDocumentManager.java:895)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntDocumentManager.initialiseMetadata(OntDocumentManager.java:853)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntDocumentManager.<init>(OntDocumentManager.java:201)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntDocumentManager.<init>(OntDocumentManager.java:183)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntDocumentManager.<init>(OntDocumentManager.java:167)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntDocumentManager.getInstance(OntDocumentManager.java:245)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntModelSpec.getDocumentManager(OntModelSpec.java:340)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.impl.OntModelImpl.getDocumentManager(OntModelImpl.java:181)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.impl.OntModelImpl.loadImports(OntModelImpl.java:1887)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.impl.OntModelImpl.<init>(OntModelImpl.java:148)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.impl.OntModelImpl.<init>(OntModelImpl.java:128)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.model.ModelFactory.createOntologyModel(ModelFactory.java:410)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.Sample.SampleApp.StartingPoint.parseOWLFile(StartingPoint.java:105)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.Sample.SampleApp.StartingPoint$3.onClick(StartingPoint.java:68)
02-24 21:29:24.273: E/AndroidRuntime(534): at android.view.View.performClick(View.java:3511)
02-24 21:29:24.273: E/AndroidRuntime(534): at android.view.View$PerformClick.run(View.java:14105)
02-24 21:29:24.273: E/AndroidRuntime(534): at android.os.Handler.handleCallback(Handler.java:605)
02-24 21:29:24.273: E/AndroidRuntime(534): at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 21:29:24.273: E/AndroidRuntime(534): at android.os.Looper.loop(Looper.java:137)
02-24 21:29:24.273: E/AndroidRuntime(534): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-24 21:29:24.273: E/AndroidRuntime(534): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 21:29:24.273: E/AndroidRuntime(534): at java.lang.reflect.Method.invoke(Method.java:511)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-24 21:29:24.273: E/AndroidRuntime(534): at dalvik.system.NativeStart.main(Native Method)
02-24 21:29:24.273: E/AndroidRuntime(534): Caused by: java.lang.NoClassDefFoundError: com.hp.hpl.jena.iri.IRIFactory
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.arp.ARPOptions.<clinit>(ARPOptions.java:60)
02-24 21:29:24.273: E/AndroidRuntime(534): ... 33 more
如何消除此异常并解析OWL文件?
答案 0 :(得分:0)
此例外的Javadoc说:
表示静态初始化程序中发生意外异常。抛出ExceptionInInitializerError以指示在评估静态初始化程序或静态变量的初始化程序期间发生异常。
然后你说:
但是,我在代码中没有使用任何静态变量。
但是,您的代码&gt;&gt;是&lt;&lt;触发某些类的静态初始化,并且静态初始化抛出未被捕获的未经检查的异常。
通常,ExceptionInInitializerError
的堆栈跟踪将包含导致问题的未经检查异常的跟踪。我没有在你包含的堆栈跟踪片段中看到这个...但我希望它在完整的堆栈跟踪中。
如果您需要进一步的帮助,请提供完整的未经编辑的堆栈跟踪,而不是一个或多个堆栈跟踪的随机片段。
<强>更新强>
堆栈跟踪的显着部分在最后:
Caused by: java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory
at com.hp.hpl.jena.util.Metadata.<clinit>(Metadata.java:26)
... 21 more
JVM无法加载(或可能初始化)org.slf4j.LoggerFactory
类。问题很可能是缺少SLF4J jar文件。
该版本的Jena的依赖关系应该在文档中解释。