3

在我的应用程序中,我创建了一个文本文件来保存读数,其值如下所示:

98、97、98、……

我需要从文本文件中获取读数并将它们保存在数组列表中。我试图实现它,但我得到了运行时异常。

代码:

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");
    }

}

猫日志:

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

我试图搜索这些错误,但没有运气,请你帮我找出错误。

在此先感谢您的帮助

4

1 回答 1

2

小猜测。在方法的开头尝试此代码onCreate()

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    index = (TextView) findViewById(R.id.textView1);

在设置 Activity 的布局之前,您当前正在尝试检索布局中的某些内容。

于 2013-02-16T23:43:40.903 回答