-2

I have the following problem and I don't know how to pass it. I searched for an answer but none had this problem. I have the following:

public class RomanianIDFrontSideRecognitionResultExtractor extends MrtdResultExtractor<RomaniaIdFrontRecognizer.Result, RomaniaIdFrontRecognizer> {

    static String nume;
    static String prenume;
    static String nr;
    static String serie;
    static String cnp;
    static String nat;
    static String locn;
    static String adresa;
    static String emis;
    static String sex;
    static String start;
    static String stop;
    static String usern;
    private Context mContext;
    static String a;
    static Toast toast;

    @SuppressLint("InlinedApi")
    @Override
    protected void extractData(RomaniaIdFrontRecognizer.Result result) {
        super.extractData(result);
        add(R.string.PPLastName, result.getLastName());
        add(R.string.PPFirstName, result.getFirstName());
        add(R.string.PPIdentityCardNumber, result.getCardNumber());
        add(R.string.PPSeries, result.getIdSeries());
        add(R.string.PPCNP, result.getCnp());
        add(R.string.PPNationality, result.getNonMRZNationality());
        add(R.string.PPPlaceOfBirth, result.getPlaceOfBirth());
        add(R.string.PPAddress, result.getAddress());
        add(R.string.PPIssuingAuthority, result.getIssuedBy());
        add(R.string.PPSex, result.getNonMRZSex());
        add(R.string.PPValidFrom, result.getValidFrom());
        add(R.string.PPValidUntil, result.getValidUntil());

        //  Toast.makeText(getApplicationContext(),result.getLastName() ,
        //         Toast.LENGTH_SHORT).show();

        //  Intent myIntent = new Intent(getApplicationContext(), ResultActivity.class);
        //    getApplicationContext().startActivity(myIntent);



        SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
        SharedPreferences.Editor editor = pref.edit();


        SharedPreferences pref2 = getApplicationContext().getSharedPreferences("MyPreff", 0);
        usern= pref2.getString("username", null);

        editor.putString("nume", result.getLastName());
        editor.putString("prenume", result.getFirstName());
        editor.putString("nr", result.getCardNumber());
        editor.putString("serie", result.getIdSeries());
        editor.putString("cnp", result.getCnp());
        editor.putString("nationalitate", result.getNonMRZNationality());
        editor.putString("locn", result.getPlaceOfBirth());
        editor.putString("adresa", result.getAddress());
        editor.putString("emis", result.getIssuedBy());
        editor.putString("sex", result.getNonMRZSex());
        editor.putString("start", String.valueOf(result.getValidFrom()));
        editor.putString("sfarsit", String.valueOf(result.getValidUntil()));// Storing string
        editor.commit(); // commit changes



    }

This part of code worked ok. I could send the variables from this activity to another one. But yesterday, the getApplicationContext() is not working anymore. Yesterday appeared to me that is deprecated and I,by mistake, hit ignore this warning. And now, the variables aren't sent anymore because of that. I just receive empty variables. How can I bypass and make getApplicationContext() work again? In other files works ok, only in this file isn't working. And it appears as being italic writing. Have you seen this kind of behaviour?

Bibu
  • 23
  • 1
  • 11
  • 1
    Possible duplicate of [get Context in non-Activity class](https://stackoverflow.com/questions/17917968/get-context-in-non-activity-class) – ADM Mar 08 '19 at 14:21
  • I saw that question,is not the same problem. – Bibu Mar 08 '19 at 14:26
  • Your question is same . `getApplicationContext()` is a method of `Context` class so you need `Context` object to call `getApplicationContext()`.. – ADM Mar 08 '19 at 14:28
  • If you would read all the question you will see that this worked. – Bibu Mar 08 '19 at 14:32

1 Answers1

1

i don't know if this would work but you can try it. Inside your extractData method add a Context context then do

SharedPreferences pref = context.getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();

then in if you want to use it another activity you call it like extractData(result,this)

Ayodele Kayode
  • 304
  • 4
  • 20