Android应用程序子类跨活动

时间:2015-04-12 21:54:40

标签: android

我遇到了NullPointerException,我似乎无法弄明白为什么。我不会说我在android中经验丰富,所以在调试这些时我似乎很容易迷失...... :(

我有一个Application子类定义如下:

public class UserData extends Application {
    private String email, firstName, lastName;

    public String getEmail() {
        return email;
    }

    public void setEmail(String str) {
        email = str;
    }

}

在一个活动(登录页面)中,我设置了电子邮件字符串:

UserData user = (UserData)getApplication();
user.setEmail(results);

当我记录getEmail()时,我看到此活动中的结果就好了。

在另一个活动(仪表板)中,我收到如下电子邮件字符串:

UserData user = (UserData)getApplication();
Log.d("Email: ", user.getEmail());

这次,user.getEmail()返回一个nullpointer。有人能指出为什么会这样吗?我已经在我的Manifest中定义了这样的子类:

<application
        android:name=".UserData"></application>

错误日志:

-12 18:10:51.261  19471-19471/habitatfinal.habitatfinal E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: habitatfinal.habitatfinal, PID: 19471
java.lang.RuntimeException: Unable to start activity ComponentInfo{habitatfinal.habitatfinal/habitatfinal.habitatfinal.dashboard}: java.lang.NullPointerException: println needs a message
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5696)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
 Caused by: java.lang.NullPointerException: println needs a message
        at android.util.Log.println_native(Native Method)
        at android.util.Log.d(Log.java:139)
        at habitatfinal.habitatfinal.dashboard.onCreate(dashboard.java:29)
        at android.app.Activity.performCreate(Activity.java:5958)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

1 个答案:

答案 0 :(得分:0)

我发现了发生了什么事。问题是onPostExecute不一定在onCreate()方法完成运行后运行。因为它们是两个不同的线程,所以在程序将意图传递到下一个活动之前,子类可能不会被初始化。

因此,对象中的变量在点击新活动之前未设置。