[英]Can't run new activity from onPostExecute
I'm having trouble starting my mainActivity after I have gotten a response from a httpUrlConnection. 从httpUrlConnection获得响应后,我无法启动mainActivity。 Here is my onPostExecute method
这是我的onPostExecute方法
protected void onPostExecute(String[] result) {
serv.httphelper.handleResults(result);
}
In the method handleResult() im handling the response code. 在方法handleResult()中,im处理响应代码。 If the response code is 200 i want to run a new method inside my loginActivity class
如果响应代码为200,我想在我的loginActivity类中运行一个新方法
public void handleResults(String[] result) {
status = result[0].toString();
instructions = result[1].toString();
jsonString = result[2];
Log.d("DEBUG", status);
if (status.equals("200")) {
serv.loginActivity.proceed();
} else if (status.equals("400")) {
serv.loginActivity.loginError();
} else if (status.equals("401")) {
serv.loginActivity.loginError();
}
}
When i try to start a new activity from the proceed() method i get a nullpointerexception 当我尝试从proceed()方法开始一个新的活动时,我得到一个nullpointerexception
public void proceed(){
startActivity(new Intent (LoginActivity.this, MainActivity.class));
Log.d("TEST", "Proceed success");
}
My service class for anyone wondering: 我为任何想知道的人提供的服务课程:
public class Service {
public static HttpHelper httphelper = new HttpHelper();
public static HttpConnect conn = new HttpConnect();
public static LoginActivity loginActivity = new LoginActivity();
} }
And here is my logCat: 这是我的logCat:
Thanks in advance! 提前致谢!
Your problem is when you start the activity at the line: 您的问题是从以下行开始活动:
startActivity(new Intent (LoginActivity.this, MainActivity.class));
Well, you creates an intent that tries to get to LoginActivity.this
which probably does not exists! 好吧,您创建了一个尝试进入
LoginActivity.this
的意图,该意图可能不存在!
Instead of this, you have to give an instance of a real activity that is currently running. 取而代之的是,您必须提供当前正在运行的真实活动的实例。
A solution that I can suggest you is to pass to proceed()
the instance of the currently running activity and put it instead of LoginActivity.this
. 我建议您使用的解决方案是将当前正在运行的活动的实例传递给
proceed()
而不是LoginActivity.this
。
In case you don't have a current activity, try to put your application's context instead and add it a flag of a new task. 如果您没有当前活动,请尝试放置应用程序的上下文,并为其添加新任务的标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.