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条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug