问题描述
- 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
请多多帮忙。
解决方案
index = (TextView) findViewById(R.id.textView1);
setContentView(R.layout.activity_main);
这两句的先后顺序是不是写反了
解决方案二:
String io;
try {
StringBuffer stringBuffer = new StringBuffer();
String line ;
File file = new File("InputText");
FileInputStream stream = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
while (( line=bufferedReader.readLine()) != null){
stringBuffer.append(line);
}
io = stringBuffer.toString();
}catch (Exception e){
e.printStackTrace();
return;
}
io_text.setText(io);
时间: 2024-10-26 09:34:50