我试图检查用户是否在上次离开应用程序之前/仍然登录之前已登录。
现在,如果我第一次启动应用程序,它会给出强制停止错误。
但是当我点击确定时,它只会继续登录屏幕。
检查用户是否已登录我正在使用' 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?
答案 0 :(得分:1)
您已从“未登录”分支中注释掉finish()
。放回去并添加return
。
代码执行在if-else之后继续,并且您正在使用findViewById()
访问视图,但您只在“登录”分支中调用setContentView()
。因此findViewById()
返回null并且在null上调用方法会导致NPE。