在实例方法内部调用 startActivity() - 导致 NullPointerException
嘿-我正在尝试从 onPostExecute() 中扩展 AsyncTask 的类调用 startActivity() 。
流程如下:
扩展 AsyncTask 的类:
protected void onPostExecute() {
Login login = new Login();
login.pushCreateNewOrChooseExistingFormActivity();
}
扩展 Activity 的类:
public void pushCreateNewOrChooseExistingFormActivity() {
// start the CreateNewOrChooseExistingForm Activity
Intent intent = new Intent(Intent.ACTION_VIEW);
**ERROR_HERE*** intent.setClassName(this, CreateNewOrChooseExistingForm.class.getName());
startActivity(intent);
}
每次我都会收到此错误:
03-17 16:04:29.579: ERROR/AndroidRuntime(1503): FATAL EXCEPTION: main
03-17 16:04:29.579: ERROR/AndroidRuntime(1503): java.lang.NullPointerException
03-17 16:04:29.579: ERROR/AndroidRuntime(1503): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
03-17 16:04:29.579: ERROR/AndroidRuntime(1503): at android.content.ComponentName.(ComponentName.java:62)
03-17 16:04:29.579: ERROR/AndroidRuntime(1503): at android.content.Intent.setClassName(Intent.java:4850)
03-17 16:04:29.579: ERROR/AndroidRuntime(1503): at com.att.AppName.Login.pushCreateNewOrChooseExistingFormActivity(Login.java:47)
对于 iOS 开发人员 - 我只是尝试将新的视图控制器推送到导航控制器的堆栈上,如 pushViewController :动画:
。显然,在这个平台上很难做到这一点。
有什么想法吗?提前致谢!
更新 - 已修复:
根据 @Falmarri 的建议,我设法解决了这个问题。
首先,我不再调用 Login login = new Login();
来创建新的登录对象。坏的。坏的。坏的。没有饼干。
相反,当准备调用 .execute()
时,本教程 (appfulcrum.com/?p=126) 建议将 applicationContext 传递给执行 AsyncTask 的类,出于我的目的,如下所示:
CallWebServiceTask task = new CallWebServiceTask();
// pass the login object to the task
task.applicationContext = login;
// execute the task in the background, passing the required params
task.execute(login);
现在,在 onPostExecute() 中,我可以像这样访问我的登录对象方法:
((Login) applicationContext).pushCreateNewOrChooseExistingFormActivity();
((Login) applicationContext).showLoginFailedAlert(result.get("httpResponseCode").toString());
...
希望这对其他人有帮助!尤其是 iOS 开发者转向 Android...
Heya - I'm trying to call startActivity() from a class that extends AsyncTask in the onPostExecute().
Here's the flow:
Class that extends AsyncTask:
protected void onPostExecute() {
Login login = new Login();
login.pushCreateNewOrChooseExistingFormActivity();
}
Class that extends Activity:
public void pushCreateNewOrChooseExistingFormActivity() {
// start the CreateNewOrChooseExistingForm Activity
Intent intent = new Intent(Intent.ACTION_VIEW);
**ERROR_HERE*** intent.setClassName(this, CreateNewOrChooseExistingForm.class.getName());
startActivity(intent);
}
And I get this error… every time:
03-17 16:04:29.579: ERROR/AndroidRuntime(1503): FATAL EXCEPTION: main
03-17 16:04:29.579: ERROR/AndroidRuntime(1503): java.lang.NullPointerException
03-17 16:04:29.579: ERROR/AndroidRuntime(1503): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
03-17 16:04:29.579: ERROR/AndroidRuntime(1503): at android.content.ComponentName.(ComponentName.java:62)
03-17 16:04:29.579: ERROR/AndroidRuntime(1503): at android.content.Intent.setClassName(Intent.java:4850)
03-17 16:04:29.579: ERROR/AndroidRuntime(1503): at com.att.AppName.Login.pushCreateNewOrChooseExistingFormActivity(Login.java:47)
For iOS developers - I'm just trying to push a new view controller on to a navigational controller's stack a la pushViewController:animated:
. Which apparently - is hard to do on this platform.
Any ideas? Thanks in advance!
UPDATE - FIXED:
per @Falmarri advice, i managed to resolve this issue.
first of all, i'm no longer calling Login login = new Login();
to create a new login object. bad. bad. bad. no cookie.
instead, when preparing to call .execute()
, this tutorial (appfulcrum.com/?p=126) suggests passing the applicationContext to the class the executes the AsyncTask, for my purposes, as shown below:
CallWebServiceTask task = new CallWebServiceTask();
// pass the login object to the task
task.applicationContext = login;
// execute the task in the background, passing the required params
task.execute(login);
now, in onPostExecute()
, i can get to my Login objects methods like so:
((Login) applicationContext).pushCreateNewOrChooseExistingFormActivity();
((Login) applicationContext).showLoginFailedAlert(result.get("httpResponseCode").toString());
...
hope this helps someone else out there! especially iOS developers transistioning over to Android...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
Login
是一个扩展Activity
的类,那么您永远不应该自己创建一个新的Login
对象,例如This is very ,非常错误,您应该返回并阅读一些 Android 教程。
If
Login
is a class that extendsActivity
, you should never, ever, ever, be creating a newLogin
object yourself such asThis is very, very wrong and you should go back and go through some of the Android tutorials.