我正在尝试使用简单的登录构建一个Android应用程序并从本教程中注销应用程序 http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
BUt log out会导致应用程序崩溃 Logcat报告
09-26 04:53:03.204: E/AndroidRuntime(1926): FATAL EXCEPTION: main
09-26 04:53:03.204: E/AndroidRuntime(1926): Process: com.learn2crack.tab, PID: 1926
09-26 04:53:03.204: E/AndroidRuntime(1926): java.lang.NullPointerException
09-26 04:53:03.204: E/AndroidRuntime(1926): at com.learn2crack.tab.Attendance$1.onClick(Attendance.java:53)
09-26 04:53:03.204: E/AndroidRuntime(1926): at android.view.View.performClick(View.java:4438)
09-26 04:53:03.204: E/AndroidRuntime(1926): at android.view.View$PerformClick.run(View.java:18422)
09-26 04:53:03.204: E/AndroidRuntime(1926): at android.os.Handler.handleCallback(Handler.java:733)
09-26 04:53:03.204: E/AndroidRuntime(1926): at android.os.Handler.dispatchMessage(Handler.java:95)
09-26 04:53:03.204: E/AndroidRuntime(1926): at android.os.Looper.loop(Looper.java:136)
09-26 04:53:03.204: E/AndroidRuntime(1926): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-26 04:53:03.204: E/AndroidRuntime(1926): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 04:53:03.204: E/AndroidRuntime(1926): at java.lang.reflect.Method.invoke(Method.java:515)
09-26 04:53:03.204: E/AndroidRuntime(1926): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-26 04:53:03.204: E/AndroidRuntime(1926): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-26 04:53:03.204: E/AndroidRuntime(1926): at dalvik.system.NativeStart.main(Native Method)
注销功能
SessionManager session;
AlertDialogManager alert = new AlertDialogManager();
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, 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);
}
调用函数的类
public class Attendance extends Fragment{
SessionManager session;
AlertDialogManager alert = new AlertDialogManager();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View attendance = inflater.inflate(R.layout.attedance, container, false);
final ImageButton logoutbtn = (ImageButton)attendance.findViewById(R.id.logoutbtn);
logoutbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// Clear the session data
// This will clear all session data and
// redirect user to LoginActivity
session.logoutUser();
}
});
return attendance;}
}
答案 0 :(得分:0)
您还没有初步化您的SessionManager类,这就是您获得NPE的原因:
public class Attendance extends Fragment{
SessionManager session;
AlertDialogManager alert = new AlertDialogManager();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View attendance = inflater.inflate(R.layout.attedance, container, false);
session = new SessionManager(); //<-- Add this
final ImageButton logoutbtn = (ImageButton)attendance.findViewById(R.id.logoutbtn);
logoutbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// Clear the session data
// This will clear all session data and
// redirect user to LoginActivity
session.logoutUser();
}
});
return attendance;
}
}
答案 1 :(得分:0)
问题是因为您尚未初始化SessionManager
课程。所以在onCreateView()
方法中初始化它。像
session = new SessionManager();
如果它有一个Context那么这个
session = new SessionManager(getActivity());