无法在我的片段内解析getActivity()。showQuestion中的showQueston参数

时间:2018-08-16 15:10:00

标签: java android android-layout android-fragments

我正在使用活动类代码来创建每个的片段。我可以成功地将Mainactivity类更改为main片段,方法是将其替换为getActivity()并在每个findmyID之前添加视图。 但是我无法将显示为红色的showquestion转换为红色,尽管我在替换Mainactivity.this之前添加了getActivity()。

这是屏幕截图 enter image description here

这是整个代码

public class question extends Fragment {
EditText question = null;
RadioButton answer1 = null;
RadioButton answer2 = null;
RadioButton answer3 = null;
RadioButton answer4 = null;
RadioGroup answers = null;
Button finish = null;
int selectedAnswer = -1;
int quesIndex = 0;
int numEvents = 0;
int selected[] = null;
int correctAns[] = null;
boolean review =false;
Button prev, next = null;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.question, container, false);


    TableLayout quizLayout = (TableLayout) view.findViewById(R.id.quizLayout);
    quizLayout.setVisibility(View.INVISIBLE);

    try {
        question = (EditText) view.findViewById(R.id.question);
        answer1 = (RadioButton) view.findViewById(R.id.a0);
        answer2 = (RadioButton) view.findViewById(R.id.a1);
        answer3 = (RadioButton) view.findViewById(R.id.a2);
        answer4 = (RadioButton) view.findViewById(R.id.a3);
        answers = (RadioGroup) view.findViewById(R.id.answers);
        RadioGroup questionLayout = (RadioGroup)view.findViewById(R.id.answers);
        Button finish = (Button)view.findViewById(R.id.finish);
        finish.setOnClickListener(finishListener);

        prev = (Button)view.findViewById(R.id.Prev);
        prev.setOnClickListener(prevListener);
        next = (Button)view.findViewById(R.id.Next);
        next.setOnClickListener(nextListener);


        selected = new int[QuizFunActivity.getQuesList().length()];
        Arrays.fill(selected, -1);
        correctAns = new int[QuizFunActivity.getQuesList().length()];
        Arrays.fill(correctAns, -1);


        this.showQuestion(0,review);

        quizLayout.setVisibility(View.VISIBLE);

    } catch (Exception e) {
        Log.e("", e.getMessage().toString(), e.getCause());
    }
    return view;
}


private void showQuestion(int qIndex,boolean review) {
    try {
        JSONObject aQues = QuizFunActivity.getQuesList().getJSONObject(qIndex);
        String quesValue = aQues.getString("Question");
        if (correctAns[qIndex] == -1) {
            String correctAnsStr = aQues.getString("CorrectAnswer");
            correctAns[qIndex] = Integer.parseInt(correctAnsStr);
        }

        question.setText(quesValue.toCharArray(), 0, quesValue.length());
        answers.check(-1);
        answer1.setTextColor(Color.WHITE);
        answer2.setTextColor(Color.WHITE);
        answer3.setTextColor(Color.WHITE);
        answer4.setTextColor(Color.WHITE);
        JSONArray ansList = aQues.getJSONArray("Answers");
        String aAns = ansList.getJSONObject(0).getString("Answer");
        answer1.setText(aAns.toCharArray(), 0, aAns.length());
        aAns = ansList.getJSONObject(1).getString("Answer");
        answer2.setText(aAns.toCharArray(), 0, aAns.length());
        aAns = ansList.getJSONObject(2).getString("Answer");
        answer3.setText(aAns.toCharArray(), 0, aAns.length());
        aAns = ansList.getJSONObject(3).getString("Answer");
        answer4.setText(aAns.toCharArray(), 0, aAns.length());
        Log.d("",selected[qIndex]+"");
        if (selected[qIndex] == 0)
            answers.check(R.id.a0);
        if (selected[qIndex] == 1)
            answers.check(R.id.a1);
        if (selected[qIndex] == 2)
            answers.check(R.id.a2);
        if (selected[qIndex] == 3)
            answers.check(R.id.a3);

        setScoreTitle();
        if (quesIndex == (QuizFunActivity.getQuesList().length()-1))
            next.setEnabled(false);

        if (quesIndex == 0)
            prev.setEnabled(false);

        if (quesIndex > 0)
            prev.setEnabled(true);

        if (quesIndex < (QuizFunActivity.getQuesList().length()-1))
            next.setEnabled(true);


        if (review) {
            Log.d("review",selected[qIndex]+""+correctAns[qIndex]);;
            if (selected[qIndex] != correctAns[qIndex]) {
                if (selected[qIndex] == 0)
                    answer1.setTextColor(Color.RED);
                if (selected[qIndex] == 1)
                    answer2.setTextColor(Color.RED);
                if (selected[qIndex] == 2)
                    answer3.setTextColor(Color.RED);
                if (selected[qIndex] == 3)
                    answer4.setTextColor(Color.RED);
            }
            if (correctAns[qIndex] == 0)
                answer1.setTextColor(Color.GREEN);
            if (correctAns[qIndex] == 1)
                answer2.setTextColor(Color.GREEN);
            if (correctAns[qIndex] == 2)
                answer3.setTextColor(Color.GREEN);
            if (correctAns[qIndex] == 3)
                answer4.setTextColor(Color.GREEN);
        }
    } catch (Exception e) {
        Log.e(this.getClass().toString(), e.getMessage(), e.getCause());
    }
}


private View.OnClickListener finishListener = new View.OnClickListener() {
    public void onClick(View v) {
        setAnswer();
        //Calculate Score
        int score = 0;
        for(int i=0; i<correctAns.length; i++){
            if ((correctAns[i] != -1) && (correctAns[i] == selected[i]))
                score++;
        }
        AlertDialog alertDialog;
        alertDialog = new AlertDialog.Builder(getActivity()).create();
        alertDialog.setTitle("Score");
        alertDialog.setMessage((score) +" out of " + (QuizFunActivity.getQuesList().length()));

        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Retake", new DialogInterface.OnClickListener(){

            public void onClick(DialogInterface dialog, int which) {
                review = false;
                quesIndex=0;
                getActivity().showQuestion(0, review);
            }
        });

        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Review", new DialogInterface.OnClickListener(){

            public void onClick(DialogInterface dialog, int which) {
                review = true;
                quesIndex=0;
               getActivity().showQuestion(0, review);
            }
        });

        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"Quit", new DialogInterface.OnClickListener(){

            public void onClick(DialogInterface dialog, int which) {
                review = false;
               getActivity().finish();
            }
        });

        alertDialog.show();

    }
};

private void setAnswer() {
    if (answer1.isChecked())
        selected[quesIndex] = 0;
    if (answer2.isChecked())
        selected[quesIndex] = 1;
    if (answer3.isChecked())
        selected[quesIndex] = 2;
    if (answer4.isChecked())
        selected[quesIndex] = 3;

    Log.d("",Arrays.toString(selected));
    Log.d("",Arrays.toString(correctAns));

}

private View.OnClickListener nextListener = new View.OnClickListener() {
    public void onClick(View v) {
        setAnswer();
        quesIndex++;
        if (quesIndex >= QuizFunActivity.getQuesList().length())
            quesIndex = QuizFunActivity.getQuesList().length() - 1;

        showQuestion(quesIndex,review);
    }
};

private View.OnClickListener prevListener = new View.OnClickListener() {
    public void onClick(View v) {
        setAnswer();
        quesIndex--;
        if (quesIndex < 0)
            quesIndex = 0;

        showQuestion(quesIndex,review);
    }
};

private void setScoreTitle() {
    getActivity().setTitle("SciQuiz3     " + (quesIndex+1)+ "/" + QuizFunActivity.getQuesList().length());
}

}

我们非常感谢您的帮助。预先感谢。

1 个答案:

答案 0 :(得分:0)

getActivity()返回Activity。因此,请在使用前进行投射。

((YourActivity) getActivity()).showQuestion(...);

确保showQuestion()方法在YourActivity中是公开的。