onPause 和 onResume 方法恢复用户上次状态时出错

Error in onPause and onResume method restoring the user last state

我正在尝试恢复用户在 webview 应用程序关闭或暂停之前的最后一页。但是在 onPauseonResume 方法中出现错误,我不知道如何修复错误 cannot resolve symbol context | cannot resolve method put

@Override
protected void onPause() {
// cannot resolve symbol context
// cannot resolve method put
    super.onPause();
    SharedPreferences prefs = context.getApplicationContext().getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
    SharedPreferences.Editor edit = prefs.edit();
    edit.put("lastUrl", exfilevbrowser.getUrl());
    edit.commit();
}

@Override
protected void onResume() {
    super.onResume();
 // cannot resolve symbol context
    if(exfilevbrowser != null) {
        SharedPreferences prefs = context.getApplicationContext().getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
        String s = prefs.getString("lastUrl","");
        if(!s.equals("")) {
            appLoadUrl(s, false);
        }
    }
}

如果您在 activity 内,直接使用 getSharedPreferences() 而不是 context.getApplicationContext().getSharedPreferences()

如果你在片段里面,使用getContext().getSharedPreferences()

然后使用edit.putString()代替edit.put()