0

我需要从存储在内部存储器中的文件中读取内容并将这些内容写入 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 报告。请通过解释错误来帮助我。

4

1 回答 1

1

我在代码中看到的唯一明显错误是您正在分配FileInputStreamas 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

FileInputStream必须用一个File对象、一个FileDescriptor对象或一个String代表File名称来初始化。

例如:

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

您可能还想在此处查看在 Android 中访问文件的方式。

于 2013-07-27T06:44:21.380 回答