Android:尝试在AsyncTask中创建一个对话框

时间:2012-12-19 18:12:34

标签: android

我正在使用AsyncTask

我使用parent来创建没有错误的意图。

创建对话框的行给出了一个 父母无法解决你们。  new parent.AlertDialog.Builder(this)

我得到的错误是父级不存在,但我在同一个方法中使用父级来调用意图

代码块      私有类SendTextOperation扩展了AsyncTask {

      @Override
      protected void onPreExecute() {

        //Update UI here

      }

      @Override
      protected String doInBackground(String... params) {
            // Talk to server here to avoid Ui hanging 
          rt=TalkToServer("http://besttechsolutions.biz/projects/bookclub/login.php");
          return(rt);
      }      

      @Override
      protected void onPostExecute(String result) {  


          if (rt.contains("ok")) 
          {

              Intent i = new Intent(parent, cChat.class);
                startActivity(i);
          }
          else
          {

                 new parent.AlertDialog.Builder(this)
                .setTitle("Game Over")
                .setMessage("Your time is up, You saved " 
                        +" Million  more people!!")
                .setNeutralButton("Try Again",new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dlg, int i)
                    {

                    }} ).show();    

          }
      }

}

3 个答案:

答案 0 :(得分:1)

要显示非活动中的AlertDialog,您需要将当前活动上下文传递给您的案例中的非活动类SendTextOperation类。

Constructor创建SendTextOperation

public class SendTextOperation extends AsyncTask<String,Void,String>{
Context context;
    public SendTextOperation(Context context) {
        this.context = context;
    }

      @Override
      protected void onPreExecute() {

        //Update UI here

      }

      @Override
      protected String doInBackground(String... params) {
            // Talk to server here to avoid Ui hanging 
          rt=TalkToServer("http://besttechsolutions.biz/projects/bookclub/login.php");
          return(rt);
      }      

      @Override
      protected void onPostExecute(String result) {  


          if (rt.contains("ok")) 
          {

              Intent i = new Intent(context, cChat.class);
                startActivity(i);
          }
          else
          {

                 new context.AlertDialog.Builder(context)
                .setTitle("Game Over")
                .setMessage("Your time is up, You saved " 
                        +" Million  more people!!")
                .setNeutralButton("Try Again",new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dlg, int i)
                    {

                    }} ).show();    

          }
      }

}

并以SendTextOperation AsyncTask开头:

SendTextOperation sendtxtasyncTask = new SendTextOperation(CurrentActivity.this);
sendtxtasyncTask.execute("");

答案 1 :(得分:0)

假设您在名为MyActivity的类中声明了该类 然后在创建Dialog时使用MyActivity.this代替。

答案 2 :(得分:0)

看起来你应该这样称呼它:

final AlertDialog.Builder builder = new AlertDialog.Builder(_context);
            builder.setMessage(_context.getString(R.string.error) + ": " + _errorMessage)
                .setTitle(_context.getString(R.string.loginError))
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setCancelable(true)
                .setPositiveButton(_context.getString(R.string.ok), null);

            final AlertDialog alert = builder.create();
            alert.show();

(我自己的示例代码)

看起来您的错误正在尝试parent.AlertDialog.Builder(this),您需要使用new AlertDialog.Builder(parent),如果父级是您的上下文。