如何通过此错误处理 class 在 Android 中启动新的 Activity?
How do I launch a new Activity in Android from this error handling class?
我对此研究得越多,就越困惑。我承认我对这个 Android 很陌生。也许有人可以根据我的具体问题向我解释这一点。
在 Android 应用程序中,我有一个 Main Activity。这是 Activity.
中的相关代码
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);
}
}
此代码接近 onCreate() 函数的末尾,旨在解决 Parse.com 我们的后端服务器的问题,会话令牌在该问题中变得无效。错误处理程序应该检查错误,并根据错误类型采取一些措施。目前我们只有一个上下文,即会话令牌错误。作为响应,它会给您一个弹出窗口,告诉您再次登录,然后将您送回可以登录的启动屏幕。
这是错误处理程序 class。
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
}
}
这里有几个问题。我知道我必须将某种上下文传递给 ParseErrorHandler 中的方法,以便它们可以在当前替换为 //... 的参数中使用。问题是我一辈子都不明白这些上下文是什么以及它们是如何工作的,而且每个在线解释都变得越来越抽象。我认为上下文本质上只是调用 Intent 或 AlertDialog 的 Activity,但这一定是不正确的。
另一个问题是 revertToSignIn() 中激活意图的最后几行无法从 ParseErrorHandler 解决。这些看起来应该是比较基本的问题,但是有人能给我解释清楚吗?
**Activity:-**
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,ActivityName.this);
}
}
**ParseClass:**
public class ParseErrorHandler {
public static void handleParseError(ParseException e,Context context) {
switch(e.getMessage()) {
case "invalid session token": handleInvalidSessionToken(context);
break;
}
}
private static void handleInvalidSessionToken(Context context) {
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(context);
}
});
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); //<<this line can't be resolved at the moment
context. finish(); //<<this line can't be resolved at the moment
}
I don't understand for the life of me what these contexts are and how
they work and every online explanation gets progressively more
abstract.
这里是我得到的关于 Contexts 的漂亮信息,会对你有所帮助。
What is 'Context' on Android?
http://developer.android.com/reference/android/content/Context.html
对于你的问题,首先在 main activity 中创建上下文,然后像
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);
}
}
现在在您的 ParseErrorHandler.java class 中创建构造函数,其中包含帮助您在所有方法中执行类似操作的上下文,
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);
}
}
ActivityMain.class中的代码是:
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().
谢谢大家。
我对此研究得越多,就越困惑。我承认我对这个 Android 很陌生。也许有人可以根据我的具体问题向我解释这一点。
在 Android 应用程序中,我有一个 Main Activity。这是 Activity.
中的相关代码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);
}
}
此代码接近 onCreate() 函数的末尾,旨在解决 Parse.com 我们的后端服务器的问题,会话令牌在该问题中变得无效。错误处理程序应该检查错误,并根据错误类型采取一些措施。目前我们只有一个上下文,即会话令牌错误。作为响应,它会给您一个弹出窗口,告诉您再次登录,然后将您送回可以登录的启动屏幕。
这是错误处理程序 class。
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
}
} 这里有几个问题。我知道我必须将某种上下文传递给 ParseErrorHandler 中的方法,以便它们可以在当前替换为 //... 的参数中使用。问题是我一辈子都不明白这些上下文是什么以及它们是如何工作的,而且每个在线解释都变得越来越抽象。我认为上下文本质上只是调用 Intent 或 AlertDialog 的 Activity,但这一定是不正确的。
另一个问题是 revertToSignIn() 中激活意图的最后几行无法从 ParseErrorHandler 解决。这些看起来应该是比较基本的问题,但是有人能给我解释清楚吗?
**Activity:-**
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,ActivityName.this);
}
}
**ParseClass:**
public class ParseErrorHandler {
public static void handleParseError(ParseException e,Context context) {
switch(e.getMessage()) {
case "invalid session token": handleInvalidSessionToken(context);
break;
}
}
private static void handleInvalidSessionToken(Context context) {
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(context);
}
});
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); //<<this line can't be resolved at the moment
context. finish(); //<<this line can't be resolved at the moment
}
I don't understand for the life of me what these contexts are and how they work and every online explanation gets progressively more abstract.
这里是我得到的关于 Contexts 的漂亮信息,会对你有所帮助。
What is 'Context' on Android?
http://developer.android.com/reference/android/content/Context.html
对于你的问题,首先在 main activity 中创建上下文,然后像
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);
}
}
现在在您的 ParseErrorHandler.java class 中创建构造函数,其中包含帮助您在所有方法中执行类似操作的上下文,
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);
}
}
ActivityMain.class中的代码是:
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().
谢谢大家。