我正在制作一个包含多项选择题的应用。每个问题都有自己的活动,例如QuestionTwoActivity
。用户每次单击按钮(例如答案A的按钮)时,都会将其移至下一个活动,例如,从QuestionTwoActivity
移至QuestionThreeActivity
。如果答案是正确的,则将其分数加3。它有10个问题,可能的总分是30。
我的应用程序的那部分工作正常,但是问题出在我想制作最终显示在GradeActivity
上的最终预览时,该预览最初仅显示用户的最终评分。我想向成绩活动中添加一个列表,以显示用户对每个问题的答案,如果用户回答正确与否,那么我制作了一个自定义arrayAdapter来填充列表,然后尝试使用每个答案的onclick方法都会根据回答的问题向arrayadapter的arraylist添加一个元素,但这导致应用程序崩溃
这是gradeActivity
的代码,仅在回答第十个问题后才会打开:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class Grade extends AppCompatActivity{
examark mExamGrade; // declaring an instance of the examark class to call getMark method
double mark; // an int object to store the exam mark in it
TextView mGradeTextView;
TextView mNameTextView;
ListView mPreviewListView;
static ArrayList<answers_preview_data> mPreviewArrayList;
PreviewAdapter mPreviewAdapter;
examark name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grade);
int colorRed = getResources().getColor(R.color.colorRed);
int colorOrange = getResources().getColor(R.color.colorOrange);
int colorYellow = getResources().getColor(R.color.colorYellow);
int colorGreen = getResources().getColor(R.color.colorGreen);
mExamGrade = new examark();
name = new examark();
mark = mExamGrade.getExamMark();// storing the exam mark in the mark variable
mGradeTextView = (TextView) findViewById(R.id.grade); // initializing the view responsible for showing the final mark
mGradeTextView.setText(String.valueOf(mark));
mNameTextView = (TextView) findViewById(R.id.name_text_view);
String nameOfQuizzer = name.getNameOfQuizTaker();
mNameTextView.setText(nameOfQuizzer);
mPreviewArrayList = new ArrayList<answers_preview_data>();
mPreviewAdapter = new PreviewAdapter(this, mPreviewArrayList);
mPreviewListView = (ListView) findViewById(R.id.preview_list);
mPreviewListView.setAdapter(mPreviewAdapter);
// a conditional statement to change the color of the grade based on how good the preformence
if ( mark > 0.0 && mark < 10.0 ) {
mGradeTextView.setTextColor(colorRed);
} else if (mark > 10.0 && mark <= 15.0) {
mGradeTextView.setTextColor(colorOrange);
} else if (mark > 15.0 && mark < 20.0) {
mGradeTextView.setTextColor(colorYellow);
} else if (mark > 20.0) {
mGradeTextView.setTextColor(colorGreen);
}
}
}
以下是onclickListner答案之一的代码:
mAnswerATextView.setText("A-Labour Party");
mAnswerATextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Grade.mPreviewArrayList.add(new answers_preview_data("1", "the answer of question A" , " the true answer of question 1", R.color.colorGreen, R.drawable.correct));
Intent openQuestion = new Intent(QuestionOne.this ,QuestionTwo.class);
startActivity(openQuestion);
mExamark.incrementMark();
}
});
单击答案按钮时,调试器菜单显示以下内容:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.xams, PID: 16254
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.admin.xams.QuestionOne$1.onClick(QuestionOne.java:52)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20270)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5667)
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:962)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
在打开包含arraylist的活动(设置为onCreate)之前,是否可以将其他活动的元素添加到arraylist中?
答案 0 :(得分:2)
您可以实现以下选项之一:
Activity
的构造函数中而不是onCreate()
方法中初始化列表答案 1 :(得分:0)
您可以遵循Singleton模式。本质上,您想创建一个全局列表,可以从任何地方访问。您可以把它放在几个地方,我建议扩展Application
并放在一个好的开始。
您可能有自己的Application
,但如果没有,则还需要在清单中注册它。
因此,我创建了一个名为MyApp
的应用程序,它是从Application
扩展而来的。这将包含我们可以从任何地方访问的答案的列表。
public class MyApp extends Application {
// A public static list of answers
public static ArrayList<answers_preview_data> answers = new ArrayList<>();
@Override
public void onCreate() {
super.onCreate();
// do other app setup ...
}
}
现在,在您的问题中,我们可以轻松地向此列表添加新答案。
public class QuestionOne extends AppCompatActivity implements View.OnClickListener {
@Override
public void onClick(View v) {
// Adding the new answer to your list.
MyApp.answers.add(new answers_preview_data());
}
}
在您的Grade类中,我们可以获取列表并在本地使用。
public class Grade extends AppCompatActivity {
private ArrayList<answers_preview_data> mPreviewArrayList;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPreviewArrayList = MyApp.answers;
// use your list ...
}
}