簡體   English   中英

從活動訪問方法,該方法將從類開始另一個活動

[英]Accessing method from activity that will start another activity from a class

嗨,我正在開發一個將使用指紋身份驗證登錄的應用程序,我創建了一個名為 FingerprintHandler 的類,並從該類調用一個方法,如果指紋身份驗證成功,該方法將啟動另一個活動,我還嘗試將上下文傳遞給方法,但我仍然得到一個空對象引用

在此處輸入圖片說明

指紋處理類

@TargetApi(Build.VERSION_CODES.M)
public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {

private Context context;

public FingerprintHandler(Context context){
    this.context = context;
}

public void startAuth(FingerprintManager fingerprintManager,  FingerprintManager.CryptoObject cryptoObject){
    CancellationSignal cancellationSignal = new CancellationSignal();
    fingerprintManager.authenticate(cryptoObject, cancellationSignal,0, this,null);
}


private void update(String s, boolean b){
    MainActivity m = new MainActivity();
    EditText etEmail,etPassword;

    TextView tvFeedback = (TextView) ((Activity)context).findViewById(R.id.tvFeedback);
    tvFeedback.setText(s);

    if(b == false) {
        tvFeedback.setTextColor(ContextCompat.getColor(context,R.color.colorAccent));
    } else {
        tvFeedback.setTextColor(ContextCompat.getColor(context,R.color.colorPrimary));
        m.login(context.getApplicationContext(),context,"user","password");

    }

}

}

MainActivity 類方法

public void login(final Context c1,final Context c, final String user, final String password) {
    dialog = new ProgressDialog(c);
    dialog.setMessage("Authenticating ...");
    dialog.setCancelable(false);
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, dBhelper.URL + "app-ess-login", new Response.Listener<String>() {
        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        public void onResponse(String response) {
            Log.d("test: ", response);
            try {
                JSONObject collections = new JSONObject(response);
                is_auth = collections.getInt("auth");
                JSONArray jsonArrayUsers = collections.getJSONArray("user");
                if(is_auth > 0) {
                    dBhelper.truncate();
                    for(int x = 0; x < jsonArrayUsers.length(); x++) {
                        JSONObject user = jsonArrayUsers.getJSONObject(x);
                        dBhelper.insertData(user.getInt("id"), user.getString("formal_name"), user.getString("email"), user.getString("job_title_name"), user.getString("schedule"));
                    }
                    dBhelper.username = user;
                    dBhelper.password = password;

                    dialog.dismiss();
                    Intent intent = new Intent(c, BundyClock.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(c, "Incorrect Email or Password", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            dialog.dismiss();
            Toast.makeText(c, "Error: " + error, Toast.LENGTH_SHORT).show();
    }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("email", user);
            params.put("password", password);
            return params;
        }
    };
    com.example.ess.AppController.getInstance().addToRequestQueue(stringRequest);
}

非常感謝

您正在調用startActivity(intent)這就是您看到錯誤的地方。 為什么會看到此錯誤?

  1. 您正在調用MainActivity m = new MainActivity(); m.login() 活動不是常規的 Java 類,您需要大量繼承(BaseContext 等)才能使其正常工作。 如果要使用該活動,則需要調用startActivity(Intent)以使其具有所有必要的繼承。

  2. 由於您沒有啟動MainActivity,因此它沒有適當的上下文。 當您在onResponse回調中調用startActivity(intent)時,它會拋出空指針異常,因為它沒有正確的上下文。

如何解決這個問題?

  1. 不要使用m.login() 您應該將此方法移至其他某個類(不是活動)。 或者讓login成為一個靜態方法,這樣它就不會綁定到 MainActivity。
  2. 使用context.startActivity(intent)因為您在登錄方法中傳遞context

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM