gpt4 book ai didi

java - Android:读取txt文件时出现NullPointerException

转载 作者:行者123 更新时间:2023-11-29 09:03:43 25 4
gpt4 key购买 nike

我正在尝试为 Android 编写一个翻译器应用程序并想读取一个 txt 文件。但是,我总是通过尝试读取文件得到 NullpointerException。我已将 txt 文件放入 assets 文件夹,但我认为该应用程序找不到它:(我的代码:

(这是第 160 行我的 VocabTrainer 类的代码:(vocsE、vocsG 是 LinkedLists 和 vocMapE、vocMapG 是作为 VocabTrainer 字段的 HasMaps))

try{
AssetManager manager;
manager = currentContext.getAssets();
InputStream input = manager.open("vocabs.txt");
InputStreamReader inputReader = new InputStreamReader(input);

BufferedReader reader = new BufferedReader(inputReader);
String word = null;

while ((word = reader.readLine()) != null) {
//Splitts the given String when there is given " "
String[] wordSplit = word.split(" ");

vocsE.add(wordSplit[0]);
vocsG.add(wordSplit[1]);

vocMapE.put(wordSplit[0], wordSplit[1]);
vocMapG.put(wordSplit[1], wordSplit[0]);
}
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}

这是 logcat 跟踪:

04-18 17:24:27.204: E/AndroidRuntime(1736): java.lang.RuntimeException: Unable to    
instantiate activity
ComponentInfo{de.dbgeppelheim.dbgvocab/de.dbgeppelheim.dbgvocab.TrainingActivity}:
java.lang.NullPointerException
04-18 17:24:27.204: E/AndroidRuntime(1736): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
android.app.ActivityThread.access$600(ActivityThread.java:141)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
android.os.Handler.dispatchMessage(Handler.java:99)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
android.os.Looper.loop(Looper.java:137)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
android.app.ActivityThread.main(ActivityThread.java:5039)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
java.lang.reflect.Method.invokeNative(Native Method)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
java.lang.reflect.Method.invoke(Method.java:511)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
dalvik.system.NativeStart.main(Native Method)
04-18 17:24:27.204: E/AndroidRuntime(1736): Caused by: java.lang.NullPointerException
04-18 17:24:27.204: E/AndroidRuntime(1736): at
android.content.ContextWrapper.getResources(ContextWrapper.java:89)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
de.dbgeppelheim.functionality.VocabTrainer.initialise(VocabTrainer.java:167)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
de.dbgeppelheim.functionality.VocabTrainer.<init>(VocabTrainer.java:40)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
de.dbgeppelheim.dbgvocab.TrainingActivity.<init>(TrainingActivity.java:25)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
java.lang.Class.newInstanceImpl(Native Method)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
java.lang.Class.newInstance(Class.java:1319)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
android.app.Instrumentation.newActivity(Instrumentation.java:1054)
04-18 17:24:27.204: E/AndroidRuntime(1736): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
04-18 17:24:27.204: E/AndroidRuntime(1736): ... 11 more

最佳答案

这个问题解释了如何做你想做的事情。按照这些步骤操作,如果仍然不起作用,请返回。

Reading a simple text file

关于java - Android:读取txt文件时出现NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16089135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com