2

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

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

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
4

2 回答 2

0

尝试将其放在 res 文件夹中。并重新运行。如果还是不行,如果问题是由于文件未找到错误引起的,请调试。

于 2013-04-18T17:13:24.897 回答
0

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

读取一个简单的文本文件

于 2013-04-18T17:29:59.560 回答