我试图在一个文字文件中读到一堆我想要用于我正在写的文字游戏的单词。此列表存储在assets目录中,是一个txt文件。但是,每当我试图打开它时,都会抛出异常。
List<String>wordList = new ArrayList<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open("wordlist.txt"))); //throwing a FileNotFoundException?
String word;
while((word=br.readLine()) != null)
wordList.add(word); //break txt file into different words, add to wordList
}
catch(IOException e) {
e.printStackTrace();
}
finally {
try {
br.close(); //stop reading
}
catch(IOException ex) {
ex.printStackTrace();
}
}
String[]words = new String[wordList.size()];
wordList.toArray(words); //make array of wordList
for(int i=0;i<words.length; i++)
Log.i("Brian", words[i]); //print out words in array
}
如果有任何帮助,请输入错误日志:
02-22 20:49:47.646: WARN/System.err(2351): java.io.FileNotFoundException: wordlist.txt
02-22 20:49:47.646: WARN/System.err(2351): at android.content.res.AssetManager.openAsset(Native Method)
02-22 20:49:47.746: WARN/System.err(2351): at android.content.res.AssetManager.open(AssetManager.java:299)
02-22 20:49:47.746: WARN/System.err(2351): at android.content.res.AssetManager.open(AssetManager.java:273)
02-22 20:49:47.756: WARN/System.err(2351): at com.bic.anagram.GameActivity.onCreate(GameActivity.java:40)
02-22 20:49:47.756: WARN/System.err(2351): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-22 20:49:47.756: WARN/System.err(2351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
02-22 20:49:47.756: WARN/System.err(2351): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
02-22 20:49:47.766: WARN/System.err(2351): at android.app.ActivityThread.access$2400(ActivityThread.java:121)
02-22 20:49:47.766: WARN/System.err(2351): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1925)
02-22 20:49:47.766: WARN/System.err(2351): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 20:49:47.776: WARN/System.err(2351): at android.os.Looper.loop(Looper.java:136)
02-22 20:49:47.776: WARN/System.err(2351): at android.app.ActivityThread.main(ActivityThread.java:4425)
02-22 20:49:47.776: WARN/System.err(2351): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 20:49:47.776: WARN/System.err(2351): at java.lang.reflect.Method.invoke(Method.java:521)
02-22 20:49:47.776: WARN/System.err(2351): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-22 20:49:47.776: WARN/System.err(2351): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-22 20:49:47.776: WARN/System.err(2351): at dalvik.system.NativeStart.main(Native Method)
02-22 20:49:47.776: WARN/dalvikvm(2351): threadid=3: thread exiting with uncaught exception (group=0x4001e280)
谢谢大家!
答案 0 :(得分:19)
检查文件是否已正确打包在.apk文件内的assets文件夹中。 (它可以作为zip文件浏览。如果需要,可以重新命名以查看内部。)
答案 1 :(得分:2)
在Maven上,Assets文件夹需要在 projectName / 内,而不是在其他示例中建议的 projectName / src / main 内。
来源:
http://jayway.github.io/maven-android-plugin/generate-sources-mojo.html