从活动中访问方法,该方法将从一个类中开始另一个活动

时间:2019-12-06 09:35:10

标签: java android

您好,我正在开发一个使用指纹身份验证登录的应用程序,我创建了一个名为FingerprintHandler的类,并从该类中调用一种方法,如果指纹身份验证成功,该方法将启动另一个活动,我也尝试通过方法的上下文,但是我仍然得到一个空对象引用

enter image description here

FingerprintHanlder类

@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);
}

非常感谢您

1 个答案:

答案 0 :(得分:1)

您正在呼叫startActivity(intent),这是您看到错误的地方。为什么会看到此错误?

  1. 您正在呼叫MainActivity m = new MainActivity();m.login()。活动不是常规的Java类,您需要大量继承(BaseContext等)才能使其正常工作。如果要使用该活动,则需要调用startActivity(Intent),使其具有所有必要的继承。

  2. 由于您不是启动 MainActivity,因此它没有适当的上下文。当您在startActivity(intent)回调中调用onResponse时,由于没有正确的上下文,它将引发Null Pointer Exception。

该如何解决?

  1. 请勿使用m.login()。您应该将此方法移至其他类(而不是活动)。或将login设为静态方法,使其与MainActivity无关。
  2. 使用context.startActivity(intent),因为您要在登录方法中传递context