来自onPostExecute()的finish()

时间:2014-07-05 11:22:14

标签: android android-asynctask

我的主要活动课程。用于接受代码并在异步任务中检查代码的对话框

public class StatusPage extends ActionBarActivity {
...
   Context con = this;
   onCreate{
      ...
      DialogBox = new Dialog(v.getContext());
      DialogBox.setContentView(R.layout.dialog_content);
      DialogBox.show();
      Button btnOk= (Button) DialogBox 
                    .findViewById(R.id.button1);
      btnOk.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
            EditText text = (EditText) DialogBox 
                            .findViewById(R.id.editText1);
            String code = text.getText().toString();
            GetStatus gsobj = new GetStatus();
            gsobj.connect(code, con);
         }
       });
     }
   }

我的异步任务类:

public class GetStatus{
...
   protected void connect(String code, Context con)
        throws InterruptedException, ExecutionException {
            new GetStatusTask(code, con)
                .execute("http://10.0.2.2:8080/connect_gs.php?code="
                    + code);
         }

   private class GetStatusTask extends AsyncTask<String, Void, String> {
      Context con;
      String code;

      public GetStatusTask(String code, Context con) {
         this.con = con;
         this code = code;
      }
      protected String doInBackground(String... urls) {
           //work in stealth mode
      }
      protected void onPostExecute(String result) {
         ...
         if(status == okay){
             //goto a new activity
             //finish the StatusPage activity so that it does not remain in stack
            Intent i = new Intent(con, HomePage.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            con.startActivity(i);
            ((Activity) con).finish();
         }
      }
   }
}

除了我得到一个“应用程序没有响应”对话框之外,一切正常,之后我可以按照自己的意愿查看主页。我在这一行得到了错误:

((Activity) con).finish();

日志:

FATAL EXCEPTION: main
Process: com.example.desigobuild2, PID: 3451
java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity
at com.example.desigobuild2.GetUser$GetUserTask.onPostExecute(GetStatus.java:165)
at com.example.desigobuild2.GetUser$GetUserTask.onPostExecute(GetStatus.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

我看到错误在于将上下文转换为Activity。我该如何解决?

2 个答案:

答案 0 :(得分:0)

我认为你的问题是你正在向普通的Activity类转换你的StatusPage的Activity,只是试试cast((StatusPage)con).finish();或使用StatusPage.this.finish();

答案 1 :(得分:0)

改变这个:

((Activity) con).finish();

((StatusPage ) con).finish();