-1

我正在填充一个ArrayList<Long>in QuestionView.java。我将它传递给Results.java然后ArrayList<Long>在课堂上 dialog调用它。Results.java

如果这没有意义,请告诉我,我将尝试进一步澄清。所有代码和 LogCat 输出如下。先感谢您!

QuestionView.java

public class QuestionView extends Activity {

    ArrayList<Long> pointsPerQuestion = new ArrayList<Long>(10);

    public void onCreate(Bundle savedInstanceState) {
        ....
    }

public ArrayList<Long> getPointsPerQuestion() {
    return pointsPerQuestion;
}

public void nextQuestion() {
    pointsPerQuestion.add(points); 
    score = score + points;
    i++;
    loadQuestion();
}

public class Timer extends CountDownTimer {     
    @Override
    public void onFinish() {
        ...
    }

    @Override
    public void onTick(long millisUntilFinished) {
        bar.setProgress((int) millisUntilFinished);
        points = (millisUntilFinished / 100) / 2;
        timeremaining.setText("Points remaining: " + points);
        if(i < 10) {
            questionNumber.setText("Question " + (i + 1) + " of 10");
        }
    }

结果.java

public class Results extends Activity {

QuestionView qv = new QuestionView();

ArrayList<Question> queryList = qv.getQueries();
ArrayList<Question> qs;
ArrayList<Long> pointsPerQuestion;
ArrayList<Button> buttonList = new ArrayList<Button>(10);

final Context context = this;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resultsmain);

    qs = getIntent().getParcelableArrayListExtra("queries");

    pointsPerQuestion = qv.getPointsPerQuestion();

    q1details.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.q1details);
            dialog.setTitle("Question 1 Details");

            ImageView image = (ImageView)dialog.findViewById(R.id.image);
            TextView question = (TextView)dialog.findViewById(R.id.question);
            TextView selectedAnswer = (TextView)dialog.findViewById(R.id.selectedAnswer);
            TextView correctAnswer = (TextView)dialog.findViewById(R.id.correctAnswer);
            TextView points = (TextView)dialog.findViewById(R.id.points);
            TextView verse = (TextView)dialog.findViewById(R.id.verse);
            Button close = (Button)dialog.findViewById(R.id.close);

            if(qs.get(0).getSelectedAnswer() == qs.get(1).getCorrectAnswer()) {
                image.setBackgroundResource(R.drawable.green_check_mark);
            } else {
                image.setBackgroundResource(R.drawable.red_x);
            }

            question.setText(qs.get(0).getQuery());
            selectedAnswer.setText("" + qs.get(0).getSelectedAnswer());
            correctAnswer.setText("" + qs.get(0).getCorrectAnswer());
            points.setText("" + pointsPerQuestion.get(0));    //Line 120
            verse.setText(qs.get(0).getVerse());        
            close.setText("Close");

            close.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        }
    });
}

LogCat 输出

02-14 15:21:13.281: E/AndroidRuntime(1519): FATAL EXCEPTION: main
02-14 15:21:13.281: E/AndroidRuntime(1519): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
02-14 15:21:13.281: E/AndroidRuntime(1519):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at java.util.ArrayList.get(ArrayList.java:304)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at matt.lyons.bibletrivia.Results$4.onClick(Results.java:120)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at android.view.View.performClick(View.java:4202)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at android.view.View$PerformClick.run(View.java:17340)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at android.os.Handler.handleCallback(Handler.java:725)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at android.os.Looper.loop(Looper.java:137)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at java.lang.reflect.Method.invokeNative(Native Method)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at java.lang.reflect.Method.invoke(Method.java:511)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-14 15:21:13.281: E/AndroidRuntime(1519):     at dalvik.system.NativeStart.main(Native Method)

编辑

即使我输入了这行代码:

long x = 666;
        pointsPerQuestion.add(x);

在我的onCreate()方法中,我得到了同样的错误。我getPointsPerQuestion()QuestionView课堂上的方法可能不起作用吗?

4

1 回答 1

1

问题是您正在使用 QuestionView 类的新实例:

QuestionView qv = new QuestionView();

然后您使用它来获取 pointsPerQuestion 数组:

pointsPerQuestion = qv.getPointsPerQuestion();

因此,当您在 Result 活动中时,您之前在 QuestionView 活动中所做的任何处理现在都消失了。您可以将数组设为公开和静态(在 QuestionView 中),但我建议不要这样做。

最好看看这个线程:Using the Android Application class to persist data

并从那里选择一种解决方案。

于 2013-02-14T16:31:22.093 回答