gpt4 book ai didi

android - 检查时出现 NullPointerException (ParseUser.getCurrentUser() != null)

转载 作者:行者123 更新时间:2023-11-29 00:09:21 25 4
gpt4 key购买 nike

我是 Parse.com 的新手,目前正在 Android 中使用 Parse 构建我的第一个项目。我正在尝试复制“Anywall”教程 (https://www.parse.com/tutorials/anywall-android) 中显示的登录工作流程,但我绝对无法让它正常工作。

每当我尝试启动该应用程序时,它都会给我一个 NullPointerException,我试图在 DispatchActivity(代码如下所示)中检查 (ParseUser.getCurrentUser() != null):

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Check if there is current user info
if (ParseUser.getCurrentUser() != null) {
// Start an intent for the logged in activity
startActivity(new Intent(this, MainActivity.class));
} else {
// Start and intent for the logged out activity
startActivity(new Intent(this, WelcomeActivity.class));
}

}

LogCat 中的堆栈跟踪如下:

07-13 00:34:54.925    6268-6268/com.appzylabs.pratik.groupreceipts W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa6142908)
07-13 00:34:54.925 6268-6268/com.appzylabs.pratik.groupreceipts E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appzylabs.pratik.groupreceipts/com.appzylabs.pratik.groupreceipts.DispatchActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.parse.Parse.getParseDir(Parse.java:304)
at com.parse.ParseObject.getFromDisk(ParseObject.java:667)
at com.parse.ParseUser$10.then(ParseUser.java:1056)
at com.parse.ParseUser$10.then(ParseUser.java:1003)
at bolts.Task$14.run(Task.java:796)
at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
at bolts.Task.completeAfterTask(Task.java:787)
at bolts.Task.continueWithTask(Task.java:599)
at bolts.Task.continueWithTask(Task.java:610)
at com.parse.ParseUser.getCurrentUserAsync(ParseUser.java:1003)
at com.parse.ParseUser.access$800(ParseUser.java:26)
at com.parse.ParseUser$9.then(ParseUser.java:996)
at com.parse.ParseUser$9.then(ParseUser.java:993)
at com.parse.TaskQueue.enqueue(TaskQueue.java:61)
at com.parse.ParseUser.getCurrentUserAsync(ParseUser.java:993)
at com.parse.ParseUser.getCurrentUser(ParseUser.java:979)
at com.parse.ParseUser.getCurrentUser(ParseUser.java:966)
at com.appzylabs.pratik.groupreceipts.DispatchActivity.onCreate(DispatchActivity.java:26)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

有些事情告诉我“ParseUser”没有正确初始化或实例化,但我的应用程序类中已经有以下代码:

public Application() {
}

@Override
public void onCreate() {
super.onCreate();

Parse.enableLocalDatastore(this);
Parse.initialize(this, "XXXX", "YYYY");

}
}

我在这里做错了什么?我只是想不通。有帮助吗?

最佳答案

嗯,我刚刚意识到这个问题。我在一个单独的 Application 类中调用 Parse.initialize() ,如下所示:

public class Application extends android.app.Application {
...
Parse.initialize(this, "XXXX", "YYYY");
...
}

但是,我没有将该类添加到我的 AndroidManifest.xml 中,因此代码从未被执行,Parse 从未被初始化,并且 ParseUser 抛出 NullPointerException。一旦我将它添加到我的 list 中,它就起作用了:

<application
android:name="com.appzylabs.pratik.groupreceipts.Application"

对不起!我希望这对面临同样问题的其他人有用。

关于android - 检查时出现 NullPointerException (ParseUser.getCurrentUser() != null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31372367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com