将内部文件内容写入ANDROID中的LISTVIEW

时间:2013-07-27 06:29:57

标签: java android file-io

我需要从存储在内部存储器中的文件中读取内容,并将这些内容写入ListView。我的代码如下:

StringBuilder sb = new StringBuilder();
FileInputStream fis = null;
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        fis.close();
    } catch(OutOfMemoryError om){
        om.printStackTrace();
    } catch(Exception ex){
        ex.printStackTrace();
    }
    @SuppressWarnings("unused")
    String result = sb.toString();
    ListView myListView = (ListView) findViewById(R.id.scores_display);
    String[] strings = new String[]{"Test1","Test2"};
    ArrayAdapter<String> myArrayAdapter= new ArrayAdapter<String>(this, R.id.scores_display,strings);
    myListView.setAdapter(myArrayAdapter);

出于调试目的,我刚刚要求将字符串数组显示在列表中。但是,当我尝试打开此活动时,我的应用程序崩溃了。 logcat错误报告粘贴在下面。

07-27 06:17:23.991: E/Trace(769): error opening trace file: No such file or directory (2)
07-27 06:17:52.536: E/AndroidRuntime(769): FATAL EXCEPTION: main
07-27 06:17:52.536: E/AndroidRuntime(769):      android.content.res.Resources$NotFoundException: Resource ID #0x7f090004 type #0x12 is not valid
07-27 06:17:52.536: E/AndroidRuntime(769):  at   android.content.res.Resources.loadXmlResourceParser(Resources.java:2144)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.content.res.Resources.getLayout(Resources.java:853)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.widget.AbsListView.obtainView(AbsListView.java:2159)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.widget.ListView.onMeasure(ListView.java:1158)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.View.measure(View.java:15518)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.View.measure(View.java:15518)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.View.measure(View.java:15518)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.View.measure(View.java:15518)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-27 06:17:52.536: E/AndroidRuntime(769):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.View.measure(View.java:15518)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.Choreographer.doFrame(Choreographer.java:532)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.os.Handler.handleCallback(Handler.java:725)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.os.Looper.loop(Looper.java:137)
07-27 06:17:52.536: E/AndroidRuntime(769):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-27 06:17:52.536: E/AndroidRuntime(769):  at java.lang.reflect.Method.invokeNative(Native Method)
07-27 06:17:52.536: E/AndroidRuntime(769):  at java.lang.reflect.Method.invoke(Method.java:511)
07-27 06:17:52.536: E/AndroidRuntime(769):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-27 06:17:52.536: E/AndroidRuntime(769):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-27 06:17:52.536: E/AndroidRuntime(769):  at dalvik.system.NativeStart.main(Native Method)

我无法理解logcat报告。请通过解释错误来帮助我。

1 个答案:

答案 0 :(得分:1)

我在代码中看到的唯一明显的错误是您将FileInputStream指定为null,然后在不进一步初始化的情况下引用它。

以下是您的问题:

FileInputStream fis = null; // initialized as null
// ... and then, in your try statement ...
BufferedReader reader = new BufferedReader(new InputStreamReader(fis, "UTF-8")); // "fis" referenced but still null
必须使用File对象,FileDescriptor对象或代表String名称的File初始化

FileInputStream

例如:

FileInputStream fis = new FileInputStream(new File("myFile.txt"));

您可能还想了解一下Android here中访问文件的方式。