gpt4 book ai didi

android - 获取 Android 应用程序的当前上下文

转载 作者:行者123 更新时间:2023-11-29 20:27:08 25 4
gpt4 key购买 nike

我在获取应用的当前上下文时遇到问题。我有一个对象,我在多个地方引用/使用它。在应用程序的任何地方使用时,如果用户的凭据已过期,它可能会返回 KEY_STATUS_FAILED。在这种情况下,我想从用户当前所在的任何页面将他们重定向到登录屏幕以重新建立凭据。我已经尝试了几个函数来获取基本上下文但没有成功(当前使用 getApplicationContext() )。感谢所有帮助!

//In common execution thread, after reading return from HTTP Post call
int stat = Integer.parseInt(checker.getString("status"));

if(stat == KEY_STATUS_FAILED){
Log.d("Process t", "Key Status Failed");
Intent i = new Intent(getApplicationContext(), LogInPage.class);
startActivity(i); //this never occurs
}

我目前正在获取它已进入关键状态失败 if 循环的日志输出。但是,我的应用程序没有重定向到 LogInPage.class。我确信这是基本的东西,我只是犯了一个愚蠢的错误,但我对此很陌生,所以感谢大家的帮助。

最佳答案

您正在提供不负责启动 Activity 的应用程序上下文。您需要使用 Activity Context 来执行此操作。

尝试使用:

Intent i = new Intent(this, LogInPage.class);
startActivity(i);

或 Intent i = new Intent(getActivity(), LogInPage.class);

或者尝试在您的 Activity 类中创建一个全局上下文变量并使用它或尝试这个:

Intent i = new Intent(((YourActivity) getActivity()), LogInPage.class);

关于android - 获取 Android 应用程序的当前上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32389640/

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