Java 对getApplicationContext()的不同响应
在我的应用程序中,我在一个类中使用上下文来处理对我的应用程序SharedReferences的所有请求,该类的相关部分是构造函数:Java 对getApplicationContext()的不同响应,java,android,sharedpreferences,Java,Android,Sharedpreferences,在我的应用程序中,我在一个类中使用上下文来处理对我的应用程序SharedReferences的所有请求,该类的相关部分是构造函数: private SharedPreferences preferences; public AppSettings(Context context){ preferences = PreferenceManager.getDefaultSharedPreferences(context); } 在我的初始活动中,我可以使用类中的其他一些方法设置和读取参数,
private SharedPreferences preferences;
public AppSettings(Context context){
preferences = PreferenceManager.getDefaultSharedPreferences(context);
}
在我的初始活动中,我可以使用类中的其他一些方法设置和读取参数,我使用getApplicationContext()从扩展AsyncTask的类的doInBackground()调用中调用上下文:
我在Eclipse中得到一个错误,它说:
类型MainActivity的方法AppSettings(上下文)未定义
我对getApplicationContext的理解是,它应该为我提供应用程序的全局上下文,但显然在每种情况下,它都返回不同的内容。如何解决此问题?错误。。你是说
AppSettings as = new AppSettings(getApplicationContext()); //<-- missed the "new"
AppSettings as=newappsettings(getApplicationContext())//是的,就是这样。两个小时的阅读和阅读,错过了。。。谢谢是的,每个人都会这样:)
public class MainActivity extends Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppSettings as = AppSettings(getApplicationContext());
}
...
}
AppSettings as = new AppSettings(getApplicationContext()); //<-- missed the "new"