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

时间:2018-03-23 19:17:49

标签: java android methods resolve

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

它说“无法解决完成方法”

1 个答案:

答案 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