简体   繁体   中英

nullpointerexception when checking if user is logged in

I'm trying to check if the user has logged on before/ is still logged in since he has last left the app.

Now if I start up the app for the first time it gives the force stop error.

But then when I click ok, it just continues to the login screen.

to check if the user is loggedin or not i am ussing a 'if else'

MainActivity.java oncreate where i check if user is logged on

@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

why do I get a nullpointerexception when starting the app?

You've commented out the finish() from the "not logged in" branch. Put it back and also add a return .

Code execution goes on after the if-else and there you're accessing views with findViewById() but you only call setContentView() in the "logged in" branch. Therefore findViewById() returns null and calling a method on null causes the NPE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM