当我尝试使用共享首选项创建会话时,我的应用程序崩溃

时间:2013-03-28 19:05:00

标签: android session sharedpreferences logcat

我尝试使用共享首选项在用户登录后创建会话。 问题是应用程序崩溃了,我不知道它为什么会发生。 我正在使用一个单独的类来使这些方法在所有活动中都可以使用..

我只是调用会话创建方法:session.createLoginSession(user);

以下是该课程

package com.example.liquifyapp;

import java.util.HashMap;
import com.example.liquifyapp.LogReg;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SessionManager {
    // Shared Preferences
    SharedPreferences pref;

    // Editor for Shared preferences
    Editor editor;

    // Context
    Context _context;

    // Shared pref mode
    int PRIVATE_MODE = 0;

    // Sharedpref file name
    private static final String PREF_NAME = "AndroidHivePref";

    // All Shared Preferences Keys
    private static final String IS_LOGIN = "IsLoggedIn";

    // User name (make variable public to access from outside)
    public static final String KEY_NAME = "name";


    // Constructor
    public SessionManager(Context context){
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    /**
     * Create login session
     * */
    public void createLoginSession(String name){
        // Storing login value as TRUE
        editor.putBoolean(IS_LOGIN, true);

        // Storing name in pref
        editor.putString(KEY_NAME, name);

        // commit changes
        editor.commit();
    }   

    /**
     * Check login method wil check user login status
     * If false it will redirect user to login page
     * Else won't do anything
     * */
    public void checkLogin(){
        // Check login status
        if(!this.isLoggedIn()){
            // user is not logged in redirect him to Login Activity
            Intent i = new Intent(_context, LogReg.class);
            // Closing all the Activities
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            // Add new Flag to start new Activity
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            // Staring Login Activity
            _context.startActivity(i);
        }

    }



    /**
     * Get stored session data
     * */
    public HashMap<String, String> getUserDetails(){
        HashMap<String, String> user = new HashMap<String, String>();
        // user name
        user.put(KEY_NAME, pref.getString(KEY_NAME, null));

        // return user
        return user;
    }

    /**
     * Clear session details
     * */
    public void logoutUser(){
        // Clearing all data from Shared Preferences
        editor.clear();
        editor.commit();

        // After logout redirect user to Loing Activity
        Intent i = new Intent(_context, LogReg.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Staring Login Activity
        _context.startActivity(i);
    }

    /**
     * Quick check for login
     * **/
    // Get Login State
    public boolean isLoggedIn(){
        return pref.getBoolean(IS_LOGIN, false);
    }
}

这是我的logcat

03-28 21:00:17.991: D/dalvikvm(14320): GC_CONCURRENT freed 41K, 14% free 7123K/8195K, paused 11ms+2ms, total 35ms
03-28 21:00:17.991: D/dalvikvm(14320): WAIT_FOR_CONCURRENT_GC blocked 17ms
03-28 21:00:18.038: D/dalvikvm(14320): GC_FOR_ALLOC freed 3K, 13% free 8533K/9735K, paused 12ms, total 12ms
03-28 21:00:18.132: D/dalvikvm(14320): GC_FOR_ALLOC freed 18K, 10% free 9970K/11015K, paused 22ms, total 23ms
03-28 21:00:18.202: D/libEGL(14320): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
03-28 21:00:18.218: D/libEGL(14320): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
03-28 21:00:18.222: D/libEGL(14320): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
03-28 21:00:18.261: D/OpenGLRenderer(14320): Enabling debug mode 0
03-28 21:00:32.354: D/AndroidRuntime(14320): Shutting down VM
03-28 21:00:32.354: W/dalvikvm(14320): threadid=1: thread exiting with uncaught exception (group=0x4169f300)
03-28 21:00:32.366: E/AndroidRuntime(14320): FATAL EXCEPTION: main
03-28 21:00:32.366: E/AndroidRuntime(14320): java.lang.NullPointerException
03-28 21:00:32.366: E/AndroidRuntime(14320):    at com.example.liquifyapp.LogReg$PostAsyncTask.onPostExecute(LogReg.java:213)
03-28 21:00:32.366: E/AndroidRuntime(14320):    at com.example.liquifyapp.LogReg$PostAsyncTask.onPostExecute(LogReg.java:1)
03-28 21:00:32.366: E/AndroidRuntime(14320):    at android.os.AsyncTask.finish(AsyncTask.java:631)
03-28 21:00:32.366: E/AndroidRuntime(14320):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-28 21:00:32.366: E/AndroidRuntime(14320):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-28 21:00:32.366: E/AndroidRuntime(14320):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 21:00:32.366: E/AndroidRuntime(14320):    at android.os.Looper.loop(Looper.java:137)
03-28 21:00:32.366: E/AndroidRuntime(14320):    at android.app.ActivityThread.main(ActivityThread.java:4931)
03-28 21:00:32.366: E/AndroidRuntime(14320):    at java.lang.reflect.Method.invokeNative(Native Method)
03-28 21:00:32.366: E/AndroidRuntime(14320):    at java.lang.reflect.Method.invoke(Method.java:511)
03-28 21:00:32.366: E/AndroidRuntime(14320):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-28 21:00:32.366: E/AndroidRuntime(14320):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-28 21:00:32.366: E/AndroidRuntime(14320):    at dalvik.system.NativeStart.main(Native Method)

继承LogReg课程http://pastebin.com/eBacDZqW

1 个答案:

答案 0 :(得分:2)

您忘记初始化session class SessionManager对象。onCreate LogReg setContentView public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_log_reg); session=new SessionManager(LogReg.this); //<< initialize here ..... 之后{}} {}}

{{1}}