无法解析完成方法——从 Android Studio 方法调用

can't resolve finish methods --call from Android Studio methods

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();
        }
}

它说 "can't resolve finish methods "

问题是您在其他 class 中显示对话框,即 UIResponsefinish()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