Android Studio 3.0.1
适用于QAnswerQuestion.java代码
List<Integer> wrongList = UIResponse.checkAnswer(list);
if (wrongList.size() == 0)
{
new AlertDialog.Builder(QAnswerQuestion.this).setTitle("Info")
.setMessage("You are awesome and all answers are correct!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).setNegativeButton("Cancel", null).show();
}
但是当我尝试将上述代码放在UIResponse.java
中时并在QAnswerQuestion.java中调用:
UIResponse.lastQuestionDialog(QAnswerQuestion.this,list);
和UIResponse.java代码是
static void lastQuestionDialog(final Context context, List<Question> list)
{
List<Integer> wrongList = UIResponse.checkAnswer(list);
if (wrongList.size() == 0)
{
new AlertDialog.Builder(context).setTitle("Info")
.setMessage("You are awesome and all answers are correct!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
finish();
}
}).setNegativeButton("Cancel", null).show();
}
}
它说“无法解决完成方法”
答案 0 :(得分:0)
问题是你在其他类UIResponse
中显示对话框。 finish()
是Activity
的方法。可以使用一个简单的解决方案。
static void lastQuestionDialog(final Context context, List<Question> list)
{
List<Integer> wrongList = UIResponse.checkAnswer(list);
if (wrongList.size() == 0)
{
new AlertDialog.Builder(context).setTitle("Info")
.setMessage("You are awesome and all answers are correct!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
((Activity)context).finish();
}
}).setNegativeButton("Cancel", null).show();
}
}
除此之外,我建议您使用回调界面通知Activity
对话框操作,以便您可以在Activity
中对其进行管理。阅读how-to-define-callbacks-in-android。