从 AsyncTask 调用 finish() 时出现 ClassCastException 异常
ClassCastException Exception while invoking finish() from AsyncTask
请帮助我是 android 的新手。我有执行一些网络任务的异步任务。完成后。它应该完成前面的 activity。所以我给 asyntask 提供上下文,然后我从那里开始
((Activity)context).finish();
它给了我这个例外
java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity
这是错误的做法吗。如果是,那么对此的最佳做法是什么。
A ClassCastException is thrown by Java when you try to cast an Object
of one data type to another .
您可以添加
onBackPressed();
Called when the activity has detected the user's press of the back
key.
或只需添加
finish(); // this.finish();
或
this.getContext().finish();
您的编码方式如前所述),而不是传递 Context
将 Activity
对象本身传递给您的 AsyncTask
这样您就不需要进行转换了。
在你的Activity
中你可以
Activity mActivity = this;
然后传递mActivity
而不是context
并直接在其上调用mActivity.finish()
。
((YourActivityName)context).finish();
请帮助我是 android 的新手。我有执行一些网络任务的异步任务。完成后。它应该完成前面的 activity。所以我给 asyntask 提供上下文,然后我从那里开始
((Activity)context).finish();
它给了我这个例外
java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity
这是错误的做法吗。如果是,那么对此的最佳做法是什么。
A ClassCastException is thrown by Java when you try to cast an Object of one data type to another .
您可以添加
onBackPressed();
Called when the activity has detected the user's press of the back key.
或只需添加
finish(); // this.finish();
或
this.getContext().finish();
您的编码方式如前所述),而不是传递 Context
将 Activity
对象本身传递给您的 AsyncTask
这样您就不需要进行转换了。
在你的Activity
中你可以
Activity mActivity = this;
然后传递mActivity
而不是context
并直接在其上调用mActivity.finish()
。
((YourActivityName)context).finish();