Momo1199 2013-02-17 01:53 采纳率: 0%
浏览 3188
已采纳

android中解析text文件

在应用中,创建了一个text文件保存数据,值大致如下:

98, 97, 98, ......

需要从text文件中获取然后保存在一个数组列表中,想实现时报出异常。

代码:

package com.example.meme;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;

public class test extends Activity{

    private static ArrayList<String> LIST=new ArrayList<String>();
    private static ArrayList<String> LIST2=new ArrayList<String>();
    TextView index;
    String[] inputArray;
    String delimiter = ", ";
    String input;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        index = (TextView) findViewById(R.id.textView1);
        setContentView(R.layout.activity_main);

//reading the values line by line and save them in the arraylist :"LIST"
            try {
            File sdcard = Environment.getExternalStorageDirectory();
            File file = new File(sdcard,"Oximeter.txt");


                BufferedReader br = new BufferedReader(new FileReader(file));  
                String line;   
                while ((line = br.readLine()) != null) {

                            LIST.add(line);

                            } }
            catch (IOException e) {
                e.printStackTrace();


            }

//parsing each line saved in the arraylist "LIST", and save the result in a new arraylist called LIST2
           for(int i=0; i<LIST.size(); i++)
            {
                input=LIST.get(i);// this will take the line
               inputArray = input.split(delimiter);//inputArray will include the readings
               for(int j=0;i<inputArray.length;j++)
               {
               LIST2.add(inputArray[j]);//readings are added to an arraylist
               }
            }
           index.setText("mamoun");
    }

}

catlog:

02-17 03:31:51.296: D/AndroidRuntime(5538): Shutting down VM
02-17 03:31:51.296: W/dalvikvm(5538): threadid=1: thread exiting with uncaught exception (group=0x40c501f8)
02-17 03:31:51.304: E/AndroidRuntime(5538): FATAL EXCEPTION: main
02-17 03:31:51.304: E/AndroidRuntime(5538): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.meme/com.example.meme.MainActivity}: java.lang.NullPointerException
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.os.Looper.loop(Looper.java:137)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.ActivityThread.main(ActivityThread.java:4512)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at java.lang.reflect.Method.invokeNative(Native Method)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at java.lang.reflect.Method.invoke(Method.java:511)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:984)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at dalvik.system.NativeStart.main(Native Method)
02-17 03:31:51.304: E/AndroidRuntime(5538): Caused by: java.lang.NullPointerException
02-17 03:31:51.304: E/AndroidRuntime(5538):     at com.example.meme.MainActivity.onCreate(MainActivity.java:73)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.Activity.performCreate(Activity.java:4465)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
02-17 03:31:51.304: E/AndroidRuntime(5538):     ... 11 more
02-17 03:33:00.507: I/Process(5673): Sending signal. PID: 5673 SIG: 9

请多多帮忙。

  • 写回答

2条回答 默认 最新

  • balmy 2013-02-17 08:45
    关注
       index = (TextView) findViewById(R.id.textView1);
            setContentView(R.layout.activity_main);
    

    这两句的先后顺序是不是写反了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常