0

我在Android上进行了自动登录,代码如下。但我得到这个错误:


在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980) 运行(RuntimeInit.java:492) I/Process:发送信号。PID:14850 SIG:9 进程 14850 终止。


这是我的欢迎活动代码:

 public class welcome_activity extends AppCompatActivity {
        SessionManager sessionManager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_welcome);
    
            sessionManager = new SessionManager(this);
            sessionManager.checkLogin();
    
            HashMap<String, String> user = sessionManager.getUserDetail();
            String mNip = user.get(sessionManager.NIP);
            if (mNip.equals(mNip)) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(welcome_activity.this, DashboardActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }, 2000);
            } else {
                Intent intent = new Intent(welcome_activity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }

这是我的会话管理器:

public class SessionManager {
    SharedPreferences sharedPreferences;
    public SharedPreferences.Editor editor;
    public Context context;
    int PRIVATE_MODE = 0;

    private static final String PREF_NAME = "LOGIN";
    private static final String LOGIN = "IS_LOGIN";
    public static final String NM_LENGKAP = "NM_LENGKAP";
    public static final String NIP = "NIP";
    public static final String PASSWORD = "PASSWORD";
    public static final String JABATAN = "JABATAN";
    public static final String OPD = "OPD";
    public static final String GAMBAR = "GAMBAR";

    public SessionManager(Context context) {
        this.context = context;
        sharedPreferences = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = sharedPreferences.edit();
    }

    public void createSession(String nm_lengkap, String nip, String password, String jabatan,String opd,String gambar) {
        editor.putBoolean(LOGIN, true);
        editor.putString(NM_LENGKAP, nm_lengkap);
        editor.putString(NIP, nip);
        editor.putString(PASSWORD, password);
        editor.putString(JABATAN, jabatan);
        editor.putString(OPD, opd);
        editor.putString(GAMBAR, gambar);
        editor.apply();

    }

    public boolean isLoggin() {
        return sharedPreferences.getBoolean(LOGIN, false);
    }

    public void checkLogin() {
        if (!this.isLoggin()) {
            Intent i = new Intent(context, LoginActivity.class);
            context.startActivity(i);
            ((DashboardActivity) context).finish();
        }
        if (!this.isLoggin()) {
            Intent i = new Intent(context, LoginActivity.class);
            context.startActivity(i);
            ((welcome_activity) context).finish();
        }
        if (!this.isLoggin()) {
            Intent i = new Intent(context, LoginActivity.class);
            context.startActivity(i);
            ((Absen_Activity1) context).finish();
        }
        if (!this.isLoggin()) {
            Intent i = new Intent(context, LoginActivity.class);
            context.startActivity(i);
            ((Absen_Activity2) context).finish();
        }
        if (!this.isLoggin()) {
            Intent i = new Intent(context, LoginActivity.class);
            context.startActivity(i);
            ((UbahPwd) context).finish();
        }
        if (!this.isLoggin()) {
            Intent i = new Intent(context, LoginActivity.class);
            context.startActivity(i);
            ((Lihat_Absen1) context).finish();
        }
        if (!this.isLoggin()) {
            Intent i = new Intent(context, LoginActivity.class);
            context.startActivity(i);
            ((Lihat_Absen2) context).finish();
        }
        if (!this.isLoggin()) {
            Intent i = new Intent(context, LoginActivity.class);
            context.startActivity(i);
            ((Sudah_Activity) context).finish();
        }
        if (!this.isLoggin()) {
            Intent i = new Intent(context, LoginActivity.class);
            context.startActivity(i);
            ((ProfileActivity) context).finish();
        }
    }

    public HashMap<String, String> getUserDetail() {
        HashMap<String, String> user = new HashMap<>();
        user.put(NM_LENGKAP, sharedPreferences.getString(NM_LENGKAP, null));
        user.put(NIP, sharedPreferences.getString(NIP, null));
        user.put(PASSWORD, sharedPreferences.getString(PASSWORD, null));
        user.put(JABATAN, sharedPreferences.getString(JABATAN, null));
        user.put(OPD, sharedPreferences.getString(OPD, null));
        user.put(GAMBAR, sharedPreferences.getString(GAMBAR , null));
        return user;
    }

    public void logout() {
        editor.clear();
        editor.commit();
        Intent i = new Intent(context, LoginActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

有人可以告诉我发生了什么吗?

4

1 回答 1

0

您正在SessionManager使用welcome_activity上下文进行初始化,但将其转换为DashboardActivity上下文。这就是它失败的原因。

不要从 SessionManager 中完成上下文(返回一个值,用它来完成内部调用活动)

您的checkLogin功能是样板文件并且具有不安全的铸造。你可以试试这个:

在会话管理器中:

public boolean checkLogin(){
     return !this.isLoggin();
}

活动中:

        sessionManager = new SessionManager(this);
        if(!sessionManager.checkLogin()){
            Intent i = new Intent(context, LoginActivity.class);
            context.startActivity(i);
            finish();
            return;
         }
于 2021-12-18T11:19:43.523 回答