我正在使用 Lucene 来阻止词汇。 我在Java应用程序(主要)上进行了测试,它运行得很好。但是,在我的Android应用程序(在onCreate中)它不起作用。有谁知道为什么? 这是我在onCreate中的代码,主要是在onCreate中旋转时的错误:
import org.tartarus.snowball.ext.PorterStemmer;
import com.veryinteligent.jarvas.R;
import android.app.Activity;
import android.os.Bundle;
public class LuceneStemmer extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wordcommands);
String term = "obviously";
PorterStemmer stemmer = new PorterStemmer();
stemmer.setCurrent(term);
stemmer.stem();
System.out.println(stemmer.getCurrent());
}
}
import org.apache.lucene.analysis.en.EnglishAnalyzer;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.tartarus.snowball.ext.PorterStemmer;
import org.tartarus.snowball.ext.PortugueseStemmer;
import com.rmtheis.yandtran.language.Language;
import com.rmtheis.yandtran.translate.Translate;
public class TranslationTester {
public static void main(String[] args) {
String term = "Obviously";
PorterStemmer stemmer = new PorterStemmer();
stemmer.setCurrent(term);
stemmer.stem();
System.out.println(stemmer.getCurrent());
}
}
这是运行时遇到的堆栈跟踪。
08-11 19:32:55.455: E/dalvikvm(1148): Could not find class 'org.tartarus.snowball.ext.PorterStemmer', referenced from method com.veryinteligent.jarvas.activity.LuceneStemmer.onCreate
08-11 19:32:55.455: W/dalvikvm(1148): VFY: unable to resolve new-instance 2031 (Lorg/tartarus/snowball/ext/PorterStemmer;) in Lcom/veryinteligent/jarvas/activity/LuceneStemmer;
08-11 19:32:55.455: D/dalvikvm(1148): VFY: replacing opcode 0x22 at 0x000b
08-11 19:32:55.455: D/dalvikvm(1148): VFY: dead code 0x000d-001f in Lcom/veryinteligent/jarvas/activity/LuceneStemmer;.onCreate (Landroid/os/Bundle;)V
08-11 19:32:55.506: D/AndroidRuntime(1148): Shutting down VM
08-11 19:32:55.506: W/dalvikvm(1148): threadid=1: thread exiting with uncaught exception (group=0xb6ff14f0)
08-11 19:32:55.506: E/AndroidRuntime(1148): FATAL EXCEPTION: main
08-11 19:32:55.506: E/AndroidRuntime(1148): java.lang.NoClassDefFoundError: org.tartarus.snowball.ext.PorterStemmer
08-11 19:32:55.506: E/AndroidRuntime(1148): at com.veryinteligent.jarvas.activity.LuceneStemmer.onCreate(LuceneStemmer.java:18)
08-11 19:32:55.506: E/AndroidRuntime(1148): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-11 19:32:55.506: E/AndroidRuntime(1148): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-11 19:32:55.506: E/AndroidRuntime(1148): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-11 19:32:55.506: E/AndroidRuntime(1148): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-11 19:32:55.506: E/AndroidRuntime(1148): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-11 19:32:55.506: E/AndroidRuntime(1148): at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 19:32:55.506: E/AndroidRuntime(1148): at android.os.Looper.loop(Looper.java:130)
08-11 19:32:55.506: E/AndroidRuntime(1148): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-11 19:32:55.506: E/AndroidRuntime(1148): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 19:32:55.506: E/AndroidRuntime(1148): at java.lang.reflect.Method.invoke(Method.java:507)
08-11 19:32:55.506: E/AndroidRuntime(1148): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-11 19:32:55.506: E/AndroidRuntime(1148): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-11 19:32:55.506: E/AndroidRuntime(1148): at dalvik.system.NativeStart.main(Native Method)
[更新]我发现我必须通过放置libs文件夹来导入我的文件。问题是我遇到错误"转换为dalvik格式失败,错误1"。这是我的控制台消息:
[2016-08-13 01:43:12 - JARVAS] Dx
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Exception parsing classes
at com.android.dx.command.dexer.Main.processClass(Main.java:752)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:718)
at com.android.dx.command.dexer.Main.access$1200(Main.java:85)
at com.android.dx.command.dexer.Main$FileBytesConsumer.processFileBytes(Main.java:1645)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
at com.android.dx.command.dexer.Main.processOne(Main.java:672)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:574)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:311)
at com.android.dx.command.dexer.Main.run(Main.java:277)
at sun.reflect.GeneratedMethodAccessor57.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.android.ide.eclipse.adt.internal.build.DexWrapper.run(DexWrapper.java:188)
at com.android.ide.eclipse.adt.internal.build.BuildHelper.executeDx(BuildHelper.java:786)
at com.android.ide.eclipse.adt.internal.build.builders.PostCompilerBuilder.build(PostCompilerBuilder.java:597)
at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:733)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:206)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:328)
at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:403)
at org.eclipse.core.internal.resources.Project$1.run(Project.java:618)
at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2345)
at org.eclipse.core.internal.resources.Project.internalBuild(Project.java:597)
at org.eclipse.core.internal.resources.Project.build(Project.java:124)
at com.android.ide.eclipse.adt.internal.project.ProjectHelper.doFullIncrementalDebugBuild(ProjectHelper.java:1143)
at com.android.ide.eclipse.adt.internal.launch.LaunchConfigDelegate.launch(LaunchConfigDelegate.java:155)
at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:858)
at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:707)
at org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:1018)
at org.eclipse.debug.internal.ui.DebugUIPlugin$8.run(DebugUIPlugin.java:1222)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
Caused by: com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
at com.android.dx.command.dexer.Main.parseClass(Main.java:764)
at com.android.dx.command.dexer.Main.access$1500(Main.java:85)
at com.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1684)
at com.android.dx.command.dexer.Main.processClass(Main.java:749)
... 32 more
[2016-08-13 01:43:12 - JARVAS] Dx 1 error; aborting
[2016-08-13 01:43:12 - JARVAS] Conversion to Dalvik format failed with error 1
我尝试了所有旧方法,有没有人知道任何新方法?
答案 0 :(得分:2)
问题非常清楚,就像在我们发现的堆栈跟踪中一样:
java.lang.NoClassDefFoundError: org.tartarus.snowball.ext.PorterStemmer.
在运行时检查您的类路径:这意味着您将org.tartarus.snowball.ext.*
及相关类带到Android运行时环境中。
因此,请检查缺少的依赖项(.jar文件)。对于Lucene版本> 4.0到5.2.1,您应该包含lucene-analyzers-common.jar
文件。