getApplicationContext()不再起作用

时间:2019-03-08 14:15:38

标签: android

我遇到以下问题,我不知道如何通过。我搜索了答案,但没有一个问题。我有以下内容:

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



    }

这部分代码可以正常工作。我可以将变量从此活动发送到另一活动。但是昨天,getApplicationContext()不再起作用。昨天出现在我面前的是已弃用的文件,我无意中忽略了此警告。现在,因此不再发送变量。我只收到空变量。如何绕过并使getApplicationContext()重新工作?在其他文件中可以正常工作,仅在该文件中不起作用。它似乎是斜体文字。您是否看到过这种行为?

1 个答案:

答案 0 :(得分:0)

我不知道这是否可行,但是您可以尝试一下。在您的extractData方法内部添加Context context然后执行

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

然后,如果您想使用它来进行其他活动,则将其称为extractData(result,this)