Java 我已经为数组编写了一个代码,但是logcat没有显示它的错误
我面临一个问题,但它没有显示在LOGCAT中,因为它不是代码错误或null 问题是,如果问题有2个答案,并且用户选择了这两个答案,那么这两个答案都算作正确答案,但是如果问题只有1个答案,即使用户选择了正确答案,游戏也会将其计算为错误答案,我会发布一个屏幕截图 我已经编写了代码,正确的答案将是粗体和红色,正如您所看到的 这是我的问题片段代码Java 我已经为数组编写了一个代码,但是logcat没有显示它的错误,java,android,Java,Android,我面临一个问题,但它没有显示在LOGCAT中,因为它不是代码错误或null 问题是,如果问题有2个答案,并且用户选择了这两个答案,那么这两个答案都算作正确答案,但是如果问题只有1个答案,即使用户选择了正确答案,游戏也会将其计算为错误答案,我会发布一个屏幕截图 我已经编写了代码,正确的答案将是粗体和红色,正如您所看到的 这是我的问题片段代码 public class QuestionFragment extends Fragment implements IQuestion {
public class QuestionFragment extends Fragment implements IQuestion {
TextView txt_question_text;
CheckBox ckbA, ckbB, ckbC, ckbD;
FrameLayout layout_image;
ProgressBar progressBar;
Question question;
int questionIndex = -1;
public QuestionFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View itemView = inflater.inflate(R.layout.fragment_question, container, false);
//GET QUESTION
questionIndex = getArguments().getInt("index", -1);
question = Common.questionList.get(questionIndex);
if (question != null) {
layout_image = (FrameLayout) itemView.findViewById(R.id.layout_image);
progressBar = (ProgressBar) itemView.findViewById(R.id.progress_bar);
if (question.isImageQuestion()) {
ImageView img_question = (ImageView) itemView.findViewById(R.id.img_question);
Picasso.get().load(question.getQuestionImage()).into(img_question, new Callback() {
@Override
public void onSuccess() {
progressBar.setVisibility(View.GONE);
}
@Override
public void onError(Exception e) {
Toast.makeText(getContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else
layout_image.setVisibility(View.GONE);
//VIEW
txt_question_text = (TextView) itemView.findViewById(R.id.txt_question_text);
txt_question_text.setText(question.getQuestionText());
ckbA = (CheckBox) itemView.findViewById(R.id.ckbA);
ckbA.setText(question.getAnswerA());
ckbA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Common.selected_values.add(ckbA.getText().toString());
else
Common.selected_values.remove(ckbA.getText().toString());
}
});
ckbB = (CheckBox) itemView.findViewById(R.id.ckbB);
ckbB.setText(question.getAnswerB());
ckbB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Common.selected_values.add(ckbB.getText().toString());
else
Common.selected_values.remove(ckbB.getText().toString());
}
});
ckbC = (CheckBox) itemView.findViewById(R.id.ckbC);
ckbC.setText(question.getAnswerC());
ckbC.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Common.selected_values.add(ckbC.getText().toString());
else
Common.selected_values.remove(ckbC.getText().toString());
}
});
ckbD = (CheckBox) itemView.findViewById(R.id.ckbD);
ckbD.setText(question.getAnswerD());
ckbD.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Common.selected_values.add(ckbD.getText().toString());
else
Common.selected_values.remove(ckbD.getText().toString());
}
});
}
return itemView;
}
@Override
public CurrentQuestion getSelectedAnswer() {
//THIS METHOD WILL RTURN STATE OF ANSWER
//RIGHT , WRONG OR NO ANSWER
CurrentQuestion currentQuestion = new CurrentQuestion(questionIndex, Common.ANSWER_TYPE.NO_ANSWER);//DEFAULT IS NO ANSWER
StringBuilder result = new StringBuilder();
if (Common.selected_values.size() > 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
for (int i = 0; i < arrayAnswer.length; i++)
if (i < arrayAnswer.length - 1)
result.append(new StringBuilder(((String) arrayAnswer[i]).substring(0, 1)).append(","));
else
result.append(new StringBuilder((String) arrayAnswer[i]).substring(0, 1));
} else if (Common.selected_values.size() == 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
result.append((String) arrayAnswer[0]).substring(0, 1);
}
if (question != null) {
//COMPARE CORRECT ANSWER FROM DATABASE WITH USER SELECTED ANSWER
if (!TextUtils.isEmpty(result)) {
if (result.toString().equals(question.getCorrectAnswer()))
currentQuestion.setType(Common.ANSWER_TYPE.RIGHT_ANSWER);
else
currentQuestion.setType(Common.ANSWER_TYPE.WRONG_ANSWER);
} else
currentQuestion.setType(Common.ANSWER_TYPE.NO_ANSWER);
}
else {
Toast.makeText(getContext(), "cannot get question", Toast.LENGTH_SHORT).show();
currentQuestion.setType(Common.ANSWER_TYPE.NO_ANSWER);
}
Common.selected_values.clear();//ALWAYS CLEAR SELECTED ANSWERS AFTER COMPARE IS DONE
return currentQuestion;
}
@Override
public void showCorrectAnswer() {
//BOLD CORRECT ANSWER
//PARTERN:A,B
String[] correctAnswer = question.getCorrectAnswer().split(",");
for (String answer : correctAnswer) {
if (answer.equals("A")) {
ckbA.setTypeface(null, Typeface.BOLD);
ckbA.setTextColor(Color.RED);
} else if (answer.equals("B")) {
ckbB.setTypeface(null, Typeface.BOLD);
ckbB.setTextColor(Color.RED);
} else if (answer.equals("C")) {
ckbC.setTypeface(null, Typeface.BOLD);
ckbC.setTextColor(Color.RED);
} else if (answer.equals("D")) {
ckbD.setTypeface(null, Typeface.BOLD);
ckbD.setTextColor(Color.RED);
}
}
}
@Override
public void disapleAnswer() {
ckbA.setEnabled(false);
ckbB.setEnabled(false);
ckbC.setEnabled(false);
ckbD.setEnabled(false);
}
@Override
public void resetQuestion() {
//ENABLE CHECKBOX
ckbA.setEnabled(true);
ckbB.setEnabled(true);
ckbC.setEnabled(true);
ckbD.setEnabled(true);
//REMOVE ALL SELECTED
ckbA.setChecked(false);
ckbB.setChecked(false);
ckbC.setChecked(false);
ckbD.setChecked(false);
//REMOVE ALL BOLD ON TEXT
ckbA.setTypeface(null, Typeface.NORMAL);
ckbA.setTextColor(Color.BLACK);
ckbB.setTypeface(null, Typeface.NORMAL);
ckbB.setTextColor(Color.BLACK);
ckbC.setTypeface(null, Typeface.NORMAL);
ckbC.setTextColor(Color.BLACK);
ckbD.setTypeface(null, Typeface.NORMAL);
ckbD.setTextColor(Color.BLACK);
}
}
公共类QuestionFragment扩展了实现IQuestion的片段{
text查看txt_问题_文本;
复选框ckbA、ckbB、ckbC、ckbD;
框架布局图;
ProgressBar ProgressBar;
问题;;
int questionIndex=-1;
公共问题片段(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图项视图=充气机。充气(R.layout.fragment\u问题,容器,错误);
//得到问题
questionIndex=getArguments().getInt(“索引”,-1);
question=Common.questionList.get(questionIndex);
如果(问题!=null){
layout\u image=(FrameLayout)itemView.findviewbyd(R.id.layout\u image);
progressBar=(progressBar)itemviewbyd(R.id.progress\u bar);
if(question.isImageQuestion()){
ImageView img_question=(ImageView)itemView.findViewById(R.id.img_question);
Picasso.get().load(question.getQuestionImage()).into(img_question,new Callback()){
@凌驾
成功时的公共无效(){
progressBar.setVisibility(View.GONE);
}
@凌驾
公共无效申报人(例外e){
Toast.makeText(getContext(),“”+e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}否则
布局_image.setVisibility(View.GONE);
//看法
txt_question_text=(TextView)itemView.findViewById(R.id.txt_question_text);
txt_question_text.setText(question.getQuestionText());
ckbA=(复选框)itemviewbyd(R.id.ckbA);
setText(question.getAnswerA());
setOnCheckedChangeListener(新的CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
如果(已检查)
Common.selected_values.add(ckbA.getText().toString());
其他的
Common.selected_values.remove(ckbA.getText().toString());
}
});
ckbB=(复选框)itemviewbyd(R.id.ckbB);
ckbB.setText(question.getAnswerB());
setOnCheckedChangeListener(新的CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
如果(已检查)
Common.selected_values.add(ckbB.getText().toString());
其他的
Common.selected_values.remove(ckbB.getText().toString());
}
});
ckbC=(复选框)itemviewbyd(R.id.ckbC);
setText(question.getAnswerC());
setOnCheckedChangeListener(新的CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
如果(已检查)
Common.selected_values.add(ckbC.getText().toString());
其他的
Common.selected_values.remove(ckbC.getText().toString());
}
});
ckbD=(复选框)itemviewbyd(R.id.ckbD);
ckbD.setText(question.getAnswerD());
setOnCheckedChangeListener(新的CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
如果(已检查)
Common.selected_values.add(ckbD.getText().toString());
其他的
Common.selected_values.remove(ckbD.getText().toString());
}
});
}
返回项目视图;
}
@凌驾
public CurrentQuestion getSelectedAnswer(){
//此方法将返回应答状态
//对,错,还是没有答案
CurrentQuestion CurrentQuestion=new CurrentQuestion(questionIndex,Common.ANSWER_TYPE.NO_ANSWER);//默认为NO ANSWER
StringBuilder结果=新建StringBuilder();
if(公共.selected_values.size()>1){
Object[]arrayAnswer=Common.selected_values.toArray();
for(int i=0;i @Override
public CurrentQuestion getSelectedAnswer() {
//THIS METHOD WILL RTURN STATE OF ANSWER
//RIGHT , WRONG OR NO ANSWER
CurrentQuestion currentQuestion = new CurrentQuestion(questionIndex, Common.ANSWER_TYPE.NO_ANSWER);//DEFAULT IS NO ANSWER
StringBuilder result = new StringBuilder();
if (Common.selected_values.size() > 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
for (int i = 0; i < arrayAnswer.length; i++)
if (i < arrayAnswer.length - 1)
result.append(new StringBuilder(((String) arrayAnswer[i]).substring(0, 1)).append(","));
else
result.append(new StringBuilder((String) arrayAnswer[i]).substring(0, 1));
} else if (Common.selected_values.size() == 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
result.append((String) arrayAnswer[0]).substring(0, 1);
}
CurrentQuestion currentQuestion = new CurrentQuestion(questionIndex, Common.ANSWER_TYPE.NO_ANSWER);//DEFAULT IS NO ANSWER
StringBuilder result = new StringBuilder();
if (Common.selected_values.size() >= 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
for (int i = 0; i < arrayAnswer.length; i++)
if (i < arrayAnswer.length - 1)
result.append(new StringBuilder(((String) arrayAnswer[i]).substring(0, 1)).append(","));
else
result.append(new StringBuilder((String) arrayAnswer[i]).substring(0, 1));
} /*else if (Common.selected_values.size() == 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
result.append((String) arrayAnswer[0]).substring(0, 1);
}*/