I am using SharedPreferences
to handle session. My getter method is returning null
which is not supposed to.
Code :
public class SessionManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREFER_NAME = "MyPref";
public static final String KEY_CURRENT_ID = "current_id";
public final String KEY_HANDLING_ID = "handling_id";
public final String KEY_HAS_PROFILE = "profile_exists";
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREFER_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void setCurrentId(int id) {
editor.putInt(KEY_CURRENT_ID, id);
editor.commit();
}
public int getCurrentId() {
return pref.getInt(KEY_CURRENT_ID, 0);
}
public int getHandlingId() {
return pref.getInt(KEY_HANDLING_ID, 0);
}
public void setHandlingId(int id) {
editor.putInt(KEY_HANDLING_ID, id);
editor.commit();
}
public boolean getHasProfile() {
return pref.getBoolean(KEY_HAS_PROFILE, false);
}
public void setHasProfile() {
boolean f;
if(AppController.getInstance().getProfile()!=null){
f=true;
}else {
f=false;
}
editor.putBoolean(KEY_HAS_PROFILE, f);
editor.commit();
}
public void GenerateAndSetId(int profile_id) {
int id=0;
if (AppController.getInstance().getProfile() != null) {
for (int i = 0; i < AppController.getInstance().getProfile().size() && (AppController.getInstance().getProfile().get(i).getId() == profile_id); i++) {
id=i;
break;
}
}
setHandlingId(id);
}
}
when I use in Activity
new SessionManager(getApplicationContext()).getHandlingId();
it returns NullPointerException
. Even if I set
new SessionManager(getApplicationContext()).setHandlingId(1);
then also getHandlingId()
returns NullPointerException
.
but if I define SharedPreferences
in my Activity class it shows the stored id.
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);pref.edit().putInt("handling_id",1).commit();
Log.d("Status new id " ," "+pref.getInt("handling_id",0));
which is 1.
I have googled a lot but unable to solve this issue.
Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.monitoryourweight.patriotic/com.monitoryourweight.patriotic.ManageProfileView}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.access$700(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.monitoryourweight.patriotic.ManageProfileView.setUpList(ManageProfileView.java:170)
at com.monitoryourweight.patriotic.ManageProfileView.init(ManageProfileView.java:70)
at com.monitoryourweight.patriotic.ManageProfileView.onCreate(ManageProfileView.java:65)
at android.app.Activity.performCreate(Activity.java:5250)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.access$700(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
ManageProfileView.java
public class ManageProfileView extends FragmentActivity implements View.OnClickListener, DialogListener {
private SeparatedListAdapter adapter;
private List<Map<String, ?>> profile, weight, option;
private ListView profile_listview;
private Button back_btn, save_btn;
private TextView list_caption;
private Profile userProfile;
private boolean[] status;
private boolean statusName = true;
private int type;
private SessionManager session;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.manage_profile);
init();
//other code
}
private void init() {
setUpList(getIntent().getExtras().getInt("type"));
session = new SessionManager(ManageProfileView.this);
//other code
}
private void setUpList(int type) {
profile_listview = (ListView) findViewById(R.id.profile_listview);
adapter = new SeparatedListAdapter(this);
this.type = type;
if (type == 2 && statusName) {
SharedPreferences pref = getApplicationContext().getSharedPreferences("MonitorWeightPref", 0);
pref.edit().putInt("handling_id", 1).commit();
Log.d("Status new 2 ", " " + pref.getInt("handling_id", 0));
new SessionManager(getApplicationContext()).getHandlingId();
session.setHandlingId(0);
//ERROR IS HERE
int x = session.getHandlingId();
userProfile = AppController.getInstance().getProfile().get(x);
final View footer = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.list_footer, null, false);
footer.setOnClickListener(this);
profile_listview.addFooterView(footer);
} else {
userProfile = new Profile();
}
}
public static Map<String, ?> createItem(String paramString1, String paramString2) {
HashMap localHashMap = new HashMap();
localHashMap.put("title", paramString1);
localHashMap.put("caption", paramString2);
return localHashMap;
}
@Override
public void onClick(View v) {
//other code
}
}