gpt4 book ai didi

java - android studio AsyncTask 无意且 context.finish 崩溃

转载 作者:行者123 更新时间:2023-12-02 08:39:26 25 4
gpt4 key购买 nike

我是 android 新手,并尝试使用 AsyncTask 从服务器获取到 mysql 的连接

protected void onPostExecute(String result) {


if(result.equalsIgnoreCase("true"))
{

Intent intent = new Intent(context,profile.class);
//not usefull
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
((Activity)context).finish();
Toast.makeText(context, "done", Toast.LENGTH_LONG).show();

}else //..... the rest of the code .....//
}

一切正常,但当结果为真时,它并不打算进行新 Activity
并使用 ((Activity)context).finish(); 它将崩溃
应用程序始终提示已完成

context 已定义 Context context ,我通过输入 thisMainActivity 获取其值

最佳答案

使用this是对 Activity 的引用,我认为您应该引用应用程序上下文来启动 Activity 。

if(result.equalsIgnoreCase("true"))
{


Intent intent = new Intent(context.getApplicationContext(),profile.class);

context.getApplicationContext().startActivity(intent);

((YourActivityName)context).finish();

Toast.makeText(context, "done", Toast.LENGTH_LONG).show();



}else //..... the rest of the code .....//

关于java - android studio AsyncTask 无意且 context.finish 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61469903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com