我是Parse.com的新手,目前正在使用Android中的Parse構建我的第一個項目。我正在嘗試複製'Anywall'教程(https://www.parse.com/tutorials/anywall-android)中顯示的登錄工作流程,但我完全沒有得到它的工作。NullPointerException當檢查(ParseUser.getCurrentUser()!= null)
每當我試着啓動應用程序,它讓我在哪裏,我想檢查在DispatchActivity(ParseUser.getCurrentUser()!= NULL)(代碼如下所示)一個NullPointerException:
@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");
}
}
我在這裏做錯了什麼?我無法弄清楚。任何幫助?
您的ParseUser登錄了嗎? – kRiZ
謝謝,但這是我的一個愚蠢的錯誤。如下所示。 –