gpt4 book ai didi

java - 检查用户是否登录时出现 nullpointerException

转载 作者:行者123 更新时间:2023-12-01 12:25:37 25 4
gpt4 key购买 nike

我正在尝试检查用户是否在上次离开应用程序之前登录过/是否仍然登录。

现在,如果我第一次启动应用程序,它会给出强制停止错误。

但是当我单击“确定”时,它只是继续到登录屏幕。

要检查用户是否已登录,我使用“if else”

MainActivity.java oncreate,我在其中检查用户是否登录

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

// Check login status in database
userFunctions = new UserFunctions();
if(userFunctions.isUserLoggedIn(getApplicationContext())){
// user already logged in show databoard
setContentView(R.layout.activity_main);
}else{
// user is not logged in show login screen
setContentView(R.layout.login);
finish();

return;
}

logcat

10-14 15:31:33.751: E/AndroidRuntime(30585): FATAL EXCEPTION: main
10-14 15:31:33.751: E/AndroidRuntime(30585): Process: info.androidhive.listviewfeed, PID: 30585
10-14 15:31:33.751: E/AndroidRuntime(30585): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.listviewfeed/com.BijCasperApp.slidingmenu.MainActivity}: java.lang.NullPointerException
10-14 15:31:33.751: E/AndroidRuntime(30585): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
10-14 15:31:33.751: E/AndroidRuntime(30585): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
10-14 15:31:33.751: E/AndroidRuntime(30585): at android.app.ActivityThread.access$900(ActivityThread.java:175)
10-14 15:31:33.751: E/AndroidRuntime(30585): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
10-14 15:31:33.751: E/AndroidRuntime(30585): at android.os.Handler.dispatchMessage(Handler.java:102)
10-14 15:31:33.751: E/AndroidRuntime(30585): at android.os.Looper.loop(Looper.java:146)
10-14 15:31:33.751: E/AndroidRuntime(30585): at android.app.ActivityThread.main(ActivityThread.java:5602)
10-14 15:31:33.751: E/AndroidRuntime(30585): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 15:31:33.751: E/AndroidRuntime(30585): at java.lang.reflect.Method.invoke(Method.java:515)
10-14 15:31:33.751: E/AndroidRuntime(30585): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
10-14 15:31:33.751: E/AndroidRuntime(30585): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
10-14 15:31:33.751: E/AndroidRuntime(30585): at dalvik.system.NativeStart.main(Native Method)
10-14 15:31:33.751: E/AndroidRuntime(30585): Caused by: java.lang.NullPointerException
10-14 15:31:33.751: E/AndroidRuntime(30585): at com.BijCasperApp.slidingmenu.MainActivity.onCreate(MainActivity.java:103)
10-14 15:31:33.751: E/AndroidRuntime(30585): at android.app.Activity.performCreate(Activity.java:5451)
10-14 15:31:33.751: E/AndroidRuntime(30585): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
10-14 15:31:33.751: E/AndroidRuntime(30585): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
10-14 15:31:33.751: E/AndroidRuntime(30585): ... 11 more

为什么启动应用程序时会出现 nullpointerException?

最佳答案

您已从“未登录”分支中注释掉了finish()。将其放回去并添加一个return

代码执行在 if-else 之后继续,您可以使用 findViewById() 访问 View ,但仅在“已登录”中调用 setContentView()分支。因此,findViewById() 返回 null,并且在 null 上调用方法会导致 NPE。

关于java - 检查用户是否登录时出现 nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26362353/

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