简体   繁体   English

从无法扩展活动的包开始意图

[英]Start intent from package which cannot extends Activity

I have some problems handling http response codes. 我在处理http响应代码时遇到一些问题。 The problem is that the app crashes because I do not give a specific function to run. 问题是该应用程序崩溃,因为我没有提供要运行的特定功能。 In this case I want to send the user back to login screen when the Httresponsecode is 401 otherwise the user can still use the app. 在这种情况下,我想在Httresponsecode401时将用户送回登录屏幕,否则用户仍可以使用该应用程序。

In my current code I have the following: 在当前代码中,我具有以下内容:

public boolean isUnauthorized(JSONObject response){
    try {
        if(response.getInt("StatusCode") == 401) {
            return true;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return false;
}

What I want is here is to call so that when calling this function app wide it will do the same on every screen. 我要在这里进行调用,以便在整个应用程序中调用此功能时,它将在每个屏幕上执行相同的操作。

Intent i = new Intent(Register.class, Register.activity);
startactivity(i);

However this isn't possible because the ResponseHandler cannot extends Activity. 但是,这是不可能的,因为ResponseHandler无法扩展Activity。 If I do this I get a stack-trace error containing looper.prepare() must be called. 如果执行此操作,则会收到包含looper.prepare()的堆栈跟踪错误。

Can you anybody tell me how I can call a new intent from here. 有人可以告诉我如何从这里提出新的意图。 The class containing above is in a folder called components and my app activities are in another folder in case it is needed for giving the right answer. 上面包含的类位于一个名为components的文件夹中,而我的应用程序活动位于另一个文件夹中,以防需要提供正确答案。

Intent i = new Intent(Register.class, Register.activity);
startactivity(i);

This should be 这应该是

Intent i = new Intent(CurrentClass.this, Activity.class);
startactivity(i);

if you have fragment 如果你有片段

Intent i = new Intent(getActivity(), Activity.class);
getActivity().startactivity(i);

You need to pass the current Activity or the Application Context as argument for your Intent. 您需要将当前的Activity或Application Context作为Intent的参数传递。

If it is inside a fragment then 如果它在碎片内,则

getActivity().startActivity(getActivity(), newActivity.class);

If it is inside a class then: 如果它在一个类中,则:

context.startActivity(context, newActivity.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM