您好我正在尝试使用带有MYSQL的JSON构建具有帐户功能的Android应用。我能够登录,但每当我退出时都会导致崩溃
onclick事件的代码段
public void onClick(DialogInterface dialog,int which) {
UserFunctions logout = new UserFunctions();
logout.logoutUser(getActivity());
这是我的用户功能类
的注销功能public boolean logoutUser(Context context){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, Login.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);
return true;
Logcat报告
09-22 16:47:11.278: E/AndroidRuntime(1455): FATAL EXCEPTION: main
09-22 16:47:11.278: E/AndroidRuntime(1455): Process: com.learn2crack.tab, PID: 1455
09-22 16:47:11.278: E/AndroidRuntime(1455): java.lang.NullPointerException
09-22 16:47:11.278: E/AndroidRuntime(1455): at com.learn2crack.library.UserFunctions.logoutUser(UserFunctions.java:115)
09-22 16:47:11.278: E/AndroidRuntime(1455): at com.learn2crack.tab.Profile$2.onClick(Profile.java:92)
09-22 16:47:11.278: E/AndroidRuntime(1455): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
09-22 16:47:11.278: E/AndroidRuntime(1455): at android.os.Handler.dispatchMessage(Handler.java:102)
09-22 16:47:11.278: E/AndroidRuntime(1455): at android.os.Looper.loop(Looper.java:136)
09-22 16:47:11.278: E/AndroidRuntime(1455): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-22 16:47:11.278: E/AndroidRuntime(1455): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 16:47:11.278: E/AndroidRuntime(1455): at java.lang.reflect.Method.invoke(Method.java:515)
09-22 16:47:11.278: E/AndroidRuntime(1455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-22 16:47:11.278: E/AndroidRuntime(1455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-22 16:47:11.278: E/AndroidRuntime(1455): at dalvik.system.NativeStart.main(Native Method)
我已经做了更多的搜索,并想出了添加此代码
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
但是PREF_NAME,PRIVATE_MODE有错误行,它说它无法解析为变量,但我读的示例不需要任何声明
答案 0 :(得分:0)
堆栈跟踪告诉您问题出在UserFunctions的第115行。
这是这个吗? (你不应该使用参数context
?)
_context.startActivity(i);
还是这个?
editor.clear();
编辑:
UserFunctions在logoutUser调用的上一行中创建。如果您未在UserFunction _context
的构造函数中初始化它们,则editor
将为null。
试试这个:
public static final String PREF_NAME = "whatever";
public boolean logoutUser(Context context){
SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, Login.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);
return true;
}
答案 1 :(得分:0)
我可以通过初始化私有模式来使其工作 int PRIVATE_MODE = 0; pomber用public static final初始化的答案String PREF_NAME =“whatever”;给了我这个主意