0

我有一个函数应该返回一个数组列表对象,但由于某种原因它卡在了 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 分钟,然后显示等待或终止的对话。我不知道如何获得有关该问题的更多信息。建议?

4

2 回答 2

0

好的,所以我觉得发布这个并浪费编码人员的时间很愚蠢!

我的问题将在其他地方找到。真正的问题是,我使用 Log.d 错误。如果文本字符串为空,似乎它没有被发送到日志: Log.d("This return","something"); Log.d("This returns nothing",""):S

于 2012-11-08T12:12:59.660 回答
0

不知道问题出在哪里,您将不得不解释“卡在退货声明中”。代码不会简单地“卡住”。

但是你会帮助自己解决很多问题。

例如,如果您创建了一个类并处理该类的实例而不是无类型的对象数组,那么您的代码将更容易编写、更容易调试和更容易维护。这样做还可以使编译器在您需要寻求帮助之前捕获很多错误:)

每当你想使用“对象”时,你应该停下来问问自己。当然,有时 Object 很有用,有时您别无选择,但它们是例外的,应该有充分的理由这样做 - 不是因为您没有花时间思考正确的解决方案。

您还应该为变量选择更好的名称并使用正确的 Java 命名约定。在这里,我将对象命名为 Thing 是因为您的代码没有提供适当的线索来说明 dalla、ddira、ddista 等是什么以及它们在注释或代码中如何相互关联(这是一件坏事——这段代码可能只有你见过,但当你一年后回来时,你会希望你做过这些事情)。

例如:

class Thing{

    double dalla = 0;
    double ddira = 0;
    double ddista = 0;
    double ddirm = 0;
    double ddistm = 0;
    double currentdist = 0;

}

然后,在您的循环或方法或其他任何内容中,

ArrayList<Thing> things = new ArrayList<Thing>();

...

Thing thing = new Thing();
for (int u = 0; u < accdirtemp.size(); u++) {

    thing.ddira += Math.pow(accdirtemp.get(u), 2);
    thing.ddista += Math.pow(accdisttemp.get(u), 2);
    thing.dalla += Math.pow(accdirtemp.get(u), 2)
            + Math.pow(accdisttemp.get(u), 2);
    thing.ddirm += accdirtemp.get(u);
    thing.ddistm += accdisttemp.get(u);
}

things.add(thing);

...

等等。现在花点时间重构您的代码,以免它变得更加难以阅读。

于 2012-11-07T13:26:47.113 回答