Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
上下文问题(Android ADT)_Android_Sharedpreferences_Adt_Android Context - Fatal编程技术网

上下文问题(Android ADT)

上下文问题(Android ADT),android,sharedpreferences,adt,android-context,Android,Sharedpreferences,Adt,Android Context,我正在尝试创建共享首选项,以便在我的应用程序中保存文本字段。每当我尝试使用上下文时,它将不允许我调用getApplicationContext(),它会给出错误(“没有为此类定义任何方法”)。每当我创建定义为null的上下文时,它都会给我一个时间错误 我的代码是: Context context = getApplicationContext(); SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES,

我正在尝试创建共享首选项,以便在我的应用程序中保存
文本字段。每当我尝试使用上下文时,它将不允许我调用
getApplicationContext()
,它会给出错误(
“没有为此类定义任何方法”
)。每当我创建定义为
null
上下文时,它都会给我一个时间错误

我的代码是:

Context context = getApplicationContext();
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

你在哪里执行这个代码?如果在活动中,您可以在需要传递
上下文的任何位置使用
this
作为
活动
扩展
上下文

在这种情况下,使用
getActivity
获取对当前
上下文的引用