在实例方法内部调用 startActivity() - 导致 NullPointerException

发布于 2024-10-22 16:23:01 字数 2210 浏览 1 评论 0原文

嘿-我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眸中客 2024-10-29 16:23:01

如果 Login 是一个扩展 Activity 的类,那么您永远不应该自己创建一个新的 Login 对象,例如

Login login = new Login();

This is very ,非常错误,您应该返回并阅读一些 Android 教程。

If Login is a class that extends Activity, you should never, ever, ever, be creating a new Login object yourself such as

Login login = new Login();

This is very, very wrong and you should go back and go through some of the Android tutorials.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文