我有这个代码在原始文件夹中的txt文件中写入一个双数字,另一个用于读取文件并将所有双数字放在数组中。我使用数组来平均文件中的双数。(双倍是我的ratingbar double) 在读取之后,在OnClickListener按钮中调用write。
(有人可以编辑这个,我通常不会用英文写,所以我确定它会有一些错误,即使在这个引用中也可能)
写:
private void writeMyArray(double rate){
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("arraymedia.txt", true)));
//.println(rate);
double ratex2 = rate * 2;
int erate = (int)ratex2;
try
{
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("myarray.txt", Context.MODE_APPEND));
outputStreamWriter.append(Integer.toString(erate));
outputStreamWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
读:
private void readMyArray(ArrayList<Double>array){
String ret = "";
try {
InputStream inputStream = this.getResources().openRawResource(R.raw.myarray);
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
int enc = 0;
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
array.set(enc, Double.parseDouble(bufferedReader.readLine()));
++enc;
}
inputStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
平均
private double media(ArrayList<Double>array)
{
double total = 0;
double media;
int a = 0;
for (int i=0; i<array.size(); i++)
{
total = total + array.get(i);
a=i;
}
media = total / a;
return media;
}
logcat中的错误
07-03 09:55:14.496 17307-17307/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.emilio.notification, PID: 17307
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.set(int, java.lang.Object)' on a null object reference
at com.example.emilio.notification.MainActivity.readMyArray(MainActivity.java:210)
at com.example.emilio.notification.MainActivity.access$200(MainActivity.java:45)
at com.example.emilio.notification.MainActivity$2.onClick(MainActivity.java:111)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
按钮点击代码
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ratingbar.setEnabled(false);
rating = ratingbar.getRating();
writeMyArray(rating);
readMyArray(arraydays);
button.setText(getText(R.string.obrigado) + "!" + media(arraydays));
//button.setText(getText(R.string.obrigado)+"!");
button.setEnabled(false);
}
});
UPTADE
当我更改array.set()时,它给了我这个错误:
07-03 10:25:53.576 25334-25334/com.example.emilio.notification E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.emilio.notification, PID: 25334
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.set(ArrayList.java:481)
at com.example.emilio.notification.MainActivity.readMyArray(MainActivity.java:210)
at com.example.emilio.notification.MainActivity.access$200(MainActivity.java:45)
at com.example.emilio.notification.MainActivity$2.onClick(MainActivity.java:111)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:2)
问题在于你的while循环
while(receiveString = bufferedReader.readLine()) != null)
这很好,因为它会检查null
。
但是在你的循环中,你会再次获得bufferedReader.readLine()
。
所以这意味着,当循环位于最后string
而非null
时。
代码将接收下一个string
,现在等于null
。
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
// The next line is the problem
// bufferedReader.readline() is either null
// or array is null
// bufferedReader.readLine() is going to be set to null on the last run.
array.set(enc, Double.parseDouble(bufferedReader.readLine()));
++enc;
}
我想你可能正在寻找这个
array.set(enc, Double.parseDouble(receiveString));
或可能
string doubleToParse = bufferedReader.readLine();
if(doubleToParse != null) {
array.set(enc, doubleToParse);
}
注意:
在第二个答案中,每次通过循环时,您将从bufferedReader
获取两行。
您可能面临的下一个问题是设置ArrayList
。
确保您已经打过电话
ArrayList<Double> arrayDays = new ArrayList<Double>();
如果你还没有填充数组。你应该打电话:
array.add(Double.parseDouble(doubleToParse));
最后一点,您必须确定您要解析的每个字符串实际上都包含Double
。
否则您将面临java.lang.NumberFormatException
。
try {
Double d = Double.parseDouble(doubleToParse);
array.add(d);
} catch (NumberFormatException ex) {
System.out.println(ex.getMessage());
}
答案 1 :(得分:0)
代码似乎很好,尝试替换
InputStream inputStream = this.getResources().openRawResource(R.raw.myarray);
到
InputStream inputStream = MainActivity.this.getResources().openRawResource(R.raw.myarray);
应该适合你。