我有一个函数应该返回一个数组列表对象,但由于某种原因它卡在了 return 语句中。return 语句之前的一切都在工作。我有一个错误,其中 ArrayLists 只包含一个使函数工作的值,但当然返回错误的数据。
package com.burninglobster.TP;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
public class Chart2Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Object graphdata[] = graphsetup();
Log.d("This message is NOT shown", "");
List<Double> distances = (List<Double>) graphdata[0];
List<Double> accall = (List<Double>) graphdata[1];
List<Double> accdir = (List<Double>) graphdata[2];
List<Double> accdist = (List<Double>) graphdata[3];
List<Double> meandir = (List<Double>) graphdata[4];
List<Double> meandist = (List<Double>) graphdata[5];
linegraph lgraph = new linegraph();
setContentView(R.layout.splot);
LinearLayout layout1 = (LinearLayout) findViewById(R.id.splot2);
layout1.addView(lgraph.getView1(this, distances, accall));
LinearLayout layout2 = (LinearLayout) findViewById(R.id.splot3);
layout2.addView(lgraph.getView2(this, distances, accdir, meandir));
LinearLayout layout3 = (LinearLayout) findViewById(R.id.splot4);
layout3.addView(lgraph.getView2(this, distances, accdist, meandist));
}
public Object[] graphsetup() {
SharedPreferences rprefs;
rprefs = getSharedPreferences("com.burninglobster.TP.tprefs",
Context.MODE_WORLD_READABLE);
double setdist = rprefs.getFloat("setdist", 0);
String[] sourcesarray = rprefs.getString("sourcesarray", "Standarddef")
.split(",");
String setdisc = sourcesarray[rprefs.getInt("spindiscpos", 0)];
DBHelper dbHelper = new DBHelper(Chart2Activity.this);
SQLiteDatabase db;
db = dbHelper.getReadableDatabase();
String graphquery = "SELECT " + DBHelper.SHOOTER + "," + DBHelper.WDIR
+ "," + DBHelper.WSTR + "," + DBHelper.SMOD + ","
+ DBHelper.DIST + "," + DBHelper.R_DIST + ","
+ DBHelper.OS_DIST + " FROM " + DBHelper.TABLE + " WHERE "
+ DBHelper.SHOOTER + "='" + setdisc + "'" + " ORDER BY "
+ DBHelper.DIST;
Cursor graphcursor = db.rawQuery(graphquery, null);
int rows = graphcursor.getCount();
int ishooter = graphcursor.getColumnIndex(DBHelper.SHOOTER);
int iwdir = graphcursor.getColumnIndex(DBHelper.WDIR);
int iwstr = graphcursor.getColumnIndex(DBHelper.WSTR);
int ismod = graphcursor.getColumnIndex(DBHelper.SMOD);
int idist = graphcursor.getColumnIndex(DBHelper.DIST);
int irdist = graphcursor.getColumnIndex(DBHelper.R_DIST);
int iosdist = graphcursor.getColumnIndex(DBHelper.OS_DIST);
List<Double> accall = new ArrayList<Double>();
List<Double> accdir = new ArrayList<Double>();
List<Double> meandir = new ArrayList<Double>();
List<Double> accdist = new ArrayList<Double>();
List<Double> meandist = new ArrayList<Double>();
List<Double> accdirtemp = new ArrayList<Double>();
List<Double> accdisttemp = new ArrayList<Double>();
List<Double> distances = new ArrayList<Double>();
double dalla = 0;
double ddira = 0;
double ddista = 0;
double ddirm = 0;
double ddistm = 0;
double currentdist = 0;
graphcursor.moveToFirst();
if (rows > 0) {
currentdist = graphcursor.getDouble(idist);
}
for (int i = 0; i < rows; i++) {
// REMOVED ERROR CAUSING SINGLE VALUE IN ARRAYLISTS:
// currentdist=graphcursor.getDouble(idist);
if (graphcursor.getDouble(idist) < (currentdist + 5)) {
accdirtemp.add(graphcursor.getDouble(irdist));
accdisttemp.add(graphcursor.getDouble(iosdist));
} else {
for (int u = 0; u < accdirtemp.size(); u++) {
ddira += Math.pow(accdirtemp.get(u), 2);
ddista += Math.pow(accdisttemp.get(u), 2);
dalla += Math.pow(accdirtemp.get(u), 2)
+ Math.pow(accdisttemp.get(u), 2);
ddirm += accdirtemp.get(u);
ddistm += accdisttemp.get(u);
}
accall.add(Math.sqrt(dalla / accdirtemp.size()));
Double.toString(Math.sqrt(dalla / accdirtemp.size())));
accdir.add(Math.sqrt(ddira / accdirtemp.size()));
accdist.add(Math.sqrt(ddista / accdirtemp.size()));
meandir.add(ddirm / accdirtemp.size());
meandist.add(ddistm / accdirtemp.size());
distances.add(currentdist);
dalla = 0;
ddira = 0;
ddirm = 0;
ddista = 0;
ddistm = 0;
currentdist = graphcursor.getDouble(idist) + 5;
accdirtemp.clear();
accdisttemp.clear();
accdirtemp.add(graphcursor.getDouble(irdist));
accdisttemp.add(graphcursor.getDouble(iosdist));
}
graphcursor.moveToNext();
}
for (int u = 0; u < accdirtemp.size(); u++) {
ddira += Math.pow(accdirtemp.get(u), 2);
ddista += Math.pow(accdisttemp.get(u), 2);
dalla += Math.pow(accdirtemp.get(u), 2)
+ Math.pow(accdisttemp.get(u), 2);
ddirm += accdirtemp.get(u);
ddistm += accdisttemp.get(u);
}
accall.add(Math.sqrt(dalla / accdirtemp.size()));
accdir.add(Math.sqrt(ddira / accdirtemp.size()));
accdist.add(Math.sqrt(ddista / accdirtemp.size()));
meandir.add(ddirm / accdirtemp.size());
meandist.add(ddistm / accdirtemp.size());
distances.add(currentdist);
db.close();
dbHelper.close();
Log.d("This message", " is shown");
return new Object[] { distances, accall, accdir, accdist, meandir,
meandist };
}
}
我有一个类似的功能可以正常工作,但我看不出有什么区别:
package com.burninglobster.TP;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
public class ChartActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Object plotdata[] = plotsetup();
Log.d("This message", " is also shown");
List<Double> rd1 = (List<Double>) plotdata[0];
List<Double> osd1 = (List<Double>) plotdata[1];
List<Double> rd2 = (List<Double>) plotdata[2];
List<Double> osd2 = (List<Double>) plotdata[3];
List<Double> rd3 = (List<Double>) plotdata[4];
List<Double> osd3 = (List<Double>) plotdata[5];
scatterplot plot = new scatterplot();
setContentView(R.layout.tplot);
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
layout.addView(plot.getView(this, rd1, osd1, rd2, osd2, rd3, osd3));
}
public Object[] plotsetup() {
SharedPreferences rprefs;
rprefs = getSharedPreferences("com.burninglobster.TP.tprefs",
Context.MODE_WORLD_READABLE);
double setdist = rprefs.getFloat("setdist", 0);
String[] sourcesarray = rprefs.getString("sourcesarray", "Standarddef")
.split(",");
String setdisc = sourcesarray[rprefs.getInt("spindiscpos", 0)];
DBHelper dbHelper = new DBHelper(ChartActivity.this);
SQLiteDatabase db;
db = dbHelper.getReadableDatabase();
String plotquery = "SELECT " + DBHelper.SHOOTER + "," + DBHelper.WDIR
+ "," + DBHelper.WSTR + "," + DBHelper.SMOD + ","
+ DBHelper.DIST + "," + DBHelper.R_DIST + ","
+ DBHelper.OS_DIST + " FROM " + DBHelper.TABLE;
Cursor plotcursor = db.rawQuery(plotquery, null);
int ishooter = plotcursor.getColumnIndex(DBHelper.SHOOTER);
int iwdir = plotcursor.getColumnIndex(DBHelper.WDIR);
int iwstr = plotcursor.getColumnIndex(DBHelper.WSTR);
int ismod = plotcursor.getColumnIndex(DBHelper.SMOD);
int idist = plotcursor.getColumnIndex(DBHelper.DIST);
int irdist = plotcursor.getColumnIndex(DBHelper.R_DIST);
int iosdist = plotcursor.getColumnIndex(DBHelper.OS_DIST);
int rows = plotcursor.getCount();
List<Double> rd1 = new ArrayList<Double>();
List<Double> osd1 = new ArrayList<Double>();
List<Double> rd2 = new ArrayList<Double>();
List<Double> osd2 = new ArrayList<Double>();
List<Double> rd3 = new ArrayList<Double>();
List<Double> osd3 = new ArrayList<Double>();
plotcursor.moveToFirst();
int disccount = 0;
int array1 = 0;
int array2 = 0;
int array3 = 0;
double cursordist;
for (int i = 0; i < rows; i++) {
if (plotcursor.getString(0).equals(setdisc)) {
cursordist = plotcursor.getDouble(idist);
if (cursordist > (setdist - 5) && cursordist < (setdist + 5)) {
rd1.add(plotcursor.getDouble(irdist));
osd1.add(plotcursor.getDouble(iosdist));
array1++;
} else if (cursordist > (setdist)
&& cursordist < (setdist + 10)) {
rd2.add(plotcursor.getDouble(irdist));
osd2.add(plotcursor.getDouble(iosdist));
array2++;
} else if (cursordist > (setdist - 10)
&& cursordist < (setdist)) {
rd3.add(plotcursor.getDouble(irdist));
osd3.add(plotcursor.getDouble(iosdist));
array3++;
}
disccount++;
}
plotcursor.moveToNext();
}
db.close();
dbHelper.close();
Log.d("This message", " is shown");
return new Object[] { rd1, osd1, rd2, osd2, rd3, osd3 };
}
}
我在 return 语句之前添加了一个“Log.d”,然后在活动中的函数调用之后添加了另一个,并且只显示了第一个。它冻结 ½-1 分钟,然后显示等待或终止的对话。我不知道如何获得有关该问题的更多信息。建议?