java - Android toast 更改偏好设置

标签 java android sharedpreferences toast

我想告诉用户,当他更改首选项窗口中的一个或多个字段时,它已成功更改,但我遇到了一些问题:

        listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
          public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
            // Implementation
              Toast.makeText(this,
                        "You have selected ",
                        Toast.LENGTH_SHORT).show();
          }
        };
        appPrefs.registerOnSharedPreferenceChangeListener(listener);

但是我收到以下错误:

The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new SharedPreferences.OnSharedPreferenceChangeListener(){}, String, int)

谁能告诉我为什么会得到这个?

最佳答案

Toast.makeText(ActivityName.this,
                    "You have selected ",
                    Toast.LENGTH_SHORT).show();

您应该使用 Activity 上下文作为第一个参数

关于java - Android toast 更改偏好设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16319080/

相关文章:

java - 线程 "main"java.sql.SQLException : near "s": syntax error 中的异常

android - DrawerLayout 阻止 EditText 上的触摸事件

java - 使用警报编辑 SharedPreferences

java - 需要一个 CDATA 事件通知 java 的 stax 解析器

java - 当响应代码为 401 时,ClientHttpResponse.getBody() 抛出 ResourceAccessException

java - 关于 Unsafe.getObjectVolatile 的使用

android - 动画列表不起作用 - 仅显示第一个元素

java - Android点击通知打开网页

android - 当应用程序运行内存不足问题时,sharedpreferences 是否变为 null

java - 创建 SharedPreferences 实用程序