函数在return语句中被卡住/冻结

时间:2012-11-07 13:01:26

标签: android arraylist return freeze

我有一个函数应该返回arraylists的对象,但由于某种原因它会卡在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',然后在活动中的函数调用之后添加了另一个'Log.d',只显示了第一个。它冻结了½-1分钟然后显示对话等待或杀死。我不知道如何获得有关该问题的更多信息。建议?

2 个答案:

答案 0 :(得分:0)

不知道问题是什么,你必须解释“卡在退货声明中”。代码不会简单地“卡住”。

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

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

每当你想要使用'对象'时,你应该停下来问自己。当然,有时候对象是有用的,有些时候你别无选择,但它们是特殊的,只应该有充分的理由 - 不是因为你没有花时间去思考正确的解决方案。

您还应该为变量选择更好的名称,并使用适当的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);

...

等等。现在花点时间重构代码,然后再进行更糟糕的噩梦阅读。

答案 1 :(得分:0)

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

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