我正在尝试通过从原始文件夹中读取 txt 文件来为我的应用程序创建一个整数数组。txt 的每一行都有编号,如下所示:
测试.txt
1
3
4
5
6
2
-9
...
我的数组是这样的Integer[] vector = {1,2,3,4,5,6,2,-9,...}
我正在部署的代码(实际上是在点击回调方法中):
public void plotSPLinfBaffle(View v) {
List<Integer> vector = new ArrayList<Integer>();
InputStream in = getResources().openRawResource(R.raw.test);
Scanner sc = new Scanner(new InputStreamReader(in));
while(sc.hasNext()){
vector.add(Integer.parseInt(sc.nextLine()));
}
Log.d("clicked" , sc.nextLine());
Integer[] splVector = vector.toArray(new Integer[0]);
}
你看,我不断收到这个运行时错误和 logcat 消息:
02-21 14:04:35.599: D/AndroidRuntime(19925): Shutting down VM
02-21 14:04:35.599: W/dalvikvm(19925): threadid=1: thread exiting with uncaught exception (group=0x40018578)
02-21 14:04:35.619: E/AndroidRuntime(19925): FATAL EXCEPTION: main
02-21 14:04:35.619: E/AndroidRuntime(19925): java.lang.IllegalStateException: Could not execute method of the activity
02-21 14:04:35.619: E/AndroidRuntime(19925): at android.view.View$1.onClick(View.java:2144)
02-21 14:04:35.619: E/AndroidRuntime(19925): at android.view.View.performClick(View.java:2485)
02-21 14:04:35.619: E/AndroidRuntime(19925): at android.view.View$PerformClick.run(View.java:9080)
02-21 14:04:35.619: E/AndroidRuntime(19925): at android.os.Handler.handleCallback(Handler.java:587)
02-21 14:04:35.619: E/AndroidRuntime(19925): at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 14:04:35.619: E/AndroidRuntime(19925): at android.os.Looper.loop(Looper.java:130)
02-21 14:04:35.619: E/AndroidRuntime(19925): at android.app.ActivityThread.main(ActivityThread.java:3687)
02-21 14:04:35.619: E/AndroidRuntime(19925): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 14:04:35.619: E/AndroidRuntime(19925): at java.lang.reflect.Method.invoke(Method.java:507)
02-21 14:04:35.619: E/AndroidRuntime(19925): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-21 14:04:35.619: E/AndroidRuntime(19925): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-21 14:04:35.619: E/AndroidRuntime(19925): at dalvik.system.NativeStart.main(Native Method)
02-21 14:04:35.619: E/AndroidRuntime(19925): Caused by: java.lang.reflect.InvocationTargetException
02-21 14:04:35.619: E/AndroidRuntime(19925): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 14:04:35.619: E/AndroidRuntime(19925): at java.lang.reflect.Method.invoke(Method.java:507)
02-21 14:04:35.619: E/AndroidRuntime(19925): at android.view.View$1.onClick(View.java:2139)
02-21 14:04:35.619: E/AndroidRuntime(19925): ... 11 more
02-21 14:04:35.619: E/AndroidRuntime(19925): Caused by: java.util.NoSuchElementException
02-21 14:04:35.619: E/AndroidRuntime(19925): at java.util.Scanner.nextLine(Scanner.java:1417)
02-21 14:04:35.619: E/AndroidRuntime(19925): at com.soulmagnet.speakerparametrix2.PlotInfBaffle.plotSPLinfBaffle(PlotInfBaffle.java:136)
02-21 14:04:35.619: E/AndroidRuntime(19925): ... 14 more
尽管如此,当在调试模式下放置断点时:
22 vector.add(Integer.parseInt(sc.nextLine()));
我可以在每次迭代中看到向量变量,但是当它完成填充某些东西时会杀死我的活动......
我只是不明白出了什么问题,这就是我寻求帮助的原因!
先感谢您!