如何在此错误处理类中启动Android中的新活动?

如何在此错误处理类中启动Android中的新活动?,android,android-intent,android-activity,parse-platform,android-context,Android,Android Intent,Android Activity,Parse Platform,Android Context,我对此研究得越多,就越感到困惑。我承认我对Android的东西还很陌生。也许有人可以根据我的具体问题向我解释这一点 public class ParseErrorHandler { public static void handleParseError(ParseException e, Context context) { switch(e.getMessage()) { case "invalid session token": handleInvalidSessio

我对此研究得越多,就越感到困惑。我承认我对Android的东西还很陌生。也许有人可以根据我的具体问题向我解释这一点

public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}
在Android应用程序中,我有一个主要的活动。以下是上述活动的相关代码

installation.saveInBackground(new SaveCallback() {
        public void done(ParseException e) {
            if (e == null) {
                Log.v("PRBTEST","Successful save of installation");
            } else {
                Log.v("PRBTEST","Unsuccesfull save of installation: " + e.getMessage());

                ParseErrorHandler.handleParseError(e);
            }
        }
public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}
这段代码位于onCreate()函数的末尾,旨在解决Parse.com(我们的后端服务器)出现的会话令牌无效的问题。错误处理程序应该检查错误,并根据错误的类型对其进行处理。目前我们只有一个上下文,即会话令牌错误。作为响应,它会给你一个弹出窗口,告诉你再次登录,并将你发送回启动屏幕,在那里你可以登录

public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}
下面是错误处理程序类

public class ParseErrorHandler {

public static void handleParseError(ParseException e) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken();
                break;
    }
}

private static void handleInvalidSessionToken() {
    AlertDialog.Builder alert = new AlertDialog.Builder(//...);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn();
                }
            });
    alert.create().show();
}

private static void revertToSignIn() {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(//..., ActivityStartup.class);
    startActivity(intent); //<<this line can't be resolved at the moment
    finish(); //<<this line can't be resolved at the moment
}
public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}
公共类ParseErrorHandler{
公共静态无效handleParseError(ParseException e){
开关(如getMessage()){
案例“无效会话令牌”:handleInvalidSessionToken();
打破
}
}
私有静态void handleInvalidSessionToken(){
AlertDialog.Builder alert=新建AlertDialog.Builder(//…);
alert.setMessage(“哎呀!您的会话不再有效。请重新登录。”)
.setCancelable(错误)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
回复信号素();
}
});
alert.create().show();
}
私有静态void revertToSignIn(){
ParseUser.logOut();
Global.loggedIn=false;
Global.user=ParseUser.getCurrentUser();
意向意向=新意向(//…,ActivityStartup.class);
开始活动(意图);//
**活动:-**
saveInBackground(新的SaveCallback(){
公共作废完成(Parsee异常){
如果(e==null){
Log.v(“PRBTEST”,“成功保存安装”);
}否则{
Log.v(“PRBTEST”,“安装完全保存失败:”+e.getMessage());
ParseErrorHandler.handleParseError(e,ActivityName.this);
}
}
**解析类:**
公共类ParseErrorHandler{
公共静态void handleParseError(解析异常e,上下文){
开关(如getMessage()){
案例“无效会话令牌”:handleInvalidSessionToken(上下文);
打破
}
}
私有静态void handleInvalidSessionToken(上下文){
AlertDialog.Builder alert=新建AlertDialog.Builder(//…);
alert.setMessage(“哎呀!您的会话不再有效。请重新登录。”)
.setCancelable(错误)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
回复信号(上下文);
}
});
alert.create().show();
}
私有静态void revertToSignIn(上下文){
ParseUser.logOut();
Global.loggedIn=false;
Global.user=ParseUser.getCurrentUser();
意向意向=新意向(上下文,ActivityStartup.class);
背景。开始触觉(意图)//
我一辈子都不明白这些上下文是什么,以及如何理解
它们起作用了,每一个在线解释都会越来越多
抽象的

public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}
这是我得到的有关上下文的信息,将帮助你

public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}

public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}

public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}
对于您的问题,首先在主活动中创建上下文,然后执行以下操作

    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_category);
        context = this;
       .
       .
       .
       .
       .
       installation.saveInBackground(new SaveCallback() {
            public void done(ParseException e) {
                if (e == null) {
                    Log.v("PRBTEST","Successful save of installation");
                } else {
                    Log.v("PRBTEST","Unsuccesfull save of installation: " + e.getMessage());

                    ParseErrorHandler handler = new ParseErrorHandler(context);
                    handler.handleParseError(context, e);
                }
            }
public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}
现在,在ParseErrorHandler.java类中创建构造函数,该类包含上下文,可帮助您在所有方法中执行类似操作

private Context context;
    public class ParseErrorHandler {

        public ParseErrorHandler(Context context) {
            this.context = context;
        }

        // your other code...

        private static void revertToSignIn() {
            ParseUser.logOut();

            Global.loggedIn = false;
            Global.user = ParseUser.getCurrentUser();

            Intent intent = new Intent(context, ActivityStartup.class);
            startActivity(intent); 
            finish(); 
        }
    }
public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}

希望这能对你有所帮助!!

根据以上两个答案,我得到了最后的代码,与他们的答案相似但不同。这段代码编译并运行得很好!当然还有一些补充

public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}
ActivityMain.class中的代码是:

public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}
 Log.v("parse installation", "updating user");
    ParseInstallation installation = ParseInstallation.getCurrentInstallation();
    installation.put("user", Global.user.getUsername());
    installation.saveInBackground(new SaveCallback() {
        public void done(ParseException e) {
            if (e == null) {
                Log.v("PRBTEST","Successful save of installation");
            } else {
                Log.v("PRBTEST","Unsuccessful save of installation: " + e.getMessage());

                ParseErrorHandler.handleParseError(e, ActivityMain.this);
            }
        }
    });

    Log.v("parse installation", "updated user: " + ParseInstallation.getCurrentInstallation().get("user"));
我没有意识到的一件大事是,我必须从上下文调用.startActivity。比如
CONTEXT.startActivity()。

public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}

谢谢大家。

将活动上下文传递给方法并调用上下文。sartActivity(intent);谢谢!我明白了,我需要从您传递的上下文中调用startActivity。这很有帮助。我仍然需要做一些更改,但这让我得到了一些工作代码。这与其他响应一样有效。我认为这一个实际上更优雅一些。在发布问题后的最后几个小时里,我已经了解了一些但这也有帮助,谢谢。
public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}