gpt4 book ai didi

android - 检查用户是否已经登录 Parse - Android

转载 作者:太空宇宙 更新时间:2023-11-03 12:31:53 24 4
gpt4 key购买 nike

我正在使用解析来注册用户并登录。我已成功完成这些模块。现在我的疑问是,我在哪里以及如何检查用户是否已经登录。如果用户已经登录,请向他展示 MainActivity,否则向他展示 HomeActivity。

目前,我在 Manifest

中将 HomeActivity 作为我的启动器
<activity
android:name=".Activities.Home"
android:label="@string/title_activity_home" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

然后我有一个 Application 类,我在其中初始化解析对象。

   public class MyClass extends Application {
public static Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();

Parse.initialize(this, "KEYS", "KEYS");

if(ParseUser.getCurrentUser().isAuthenticated()){
Intent intent = new Intent(context,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{
Intent intent = new Intent(context,Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}

}
}

我在 logcat 中收到此错误

java.lang.RuntimeException: Unable to create application com.artqueen.aahaan.Aahaan: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.parse.ParseUser.isAuthenticated()' on a null object reference
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5255)
at android.app.ActivityThread.access$1600(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1536)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6141)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.parse.ParseUser.isAuthenticated()' on a null object reference
at com.artqueen.aahaan.Aahaan.onCreate(Aahaan.java:33)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1020)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5252)
            at android.app.ActivityThread.access$1600(ActivityThread.java:182)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1536)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6141)
            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:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

我如何实现这一点?提前致谢。

最佳答案

来自 Parse 文档:

ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
// do stuff with the user
} else {
// show the signup or login screen
}

你试过吗?

关于android - 检查用户是否已经登录 Parse - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31237531/

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