0

ユーザーがログインしたら、共有設定を使用してセッションを作成しようとしています。問題は、アプリケーションがクラッシュしていることです。原因がわかりません。私は別のクラスを使用して、すべてのアクティビティでメソッドを利用できるようにしています..

そのようなセッション作成メソッドを呼び出すだけです: 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);
    }
}

そして、ここに私のログキャットがあります:

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)

そしてheres LogRegクラスhttp://pastebin.com/eBacDZqW

4

1 に答える 1

2

sessionクラスのオブジェクトを初期化するのを忘れています。次のようにActivity のメソッドSessionManager内で初期化します :onCreateLogRegsetContentView

 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
        .....
于 2013-03-28T19:19:17.463 に答える