无法从ContextWrapper-Android类型对非静态方法getApplicationContext()进行静态引用
我创建了一个用于存储吐司主题和创建吐司的类。我目前正在一个片段中测试它,所以我想它需要是静态的。问题是 我不知道如何传递应用程序上下文。我得到这个错误:无法从ContextWrapper-Android类型对非静态方法getApplicationContext()进行静态引用,android,fragment,android-fragmentactivity,Android,Fragment,Android Fragmentactivity,我创建了一个用于存储吐司主题和创建吐司的类。我目前正在一个片段中测试它,所以我想它需要是静态的。问题是 我不知道如何传递应用程序上下文。我得到这个错误: Cannot make a static reference to the non-static method getApplicationContext() from the type ContextWrapper 我理解为什么会出现这个错误,但我不知道正确的方法。 我在论坛上搜索过类似的错误,但没有找到答案 public class Ma
Cannot make a static reference to the non-static method getApplicationContext() from the type ContextWrapper
我理解为什么会出现这个错误,但我不知道正确的方法。
我在论坛上搜索过类似的错误,但没有找到答案
public class MainActivity extends FragmentActivity
implements ActionBar.OnNavigationListener {
private static DWAWebGet webGet = new DWAWebGet();
private static DWAToaster toaster =
new DWAToaster(getApplicationContext())
.makeToast("error",Toast.LENGTH_LONG,Gravity.TOP,0,0)
.makeToast("alert",Toast.LENGTH_SHORT,Gravity.TOP,0,0)
.makeToast("msg",Toast.LENGTH_SHORT,Gravity.TOP,0,0);
...
public static class SectionFragment_sign_in extends Fragment {
...
public void click_register_button(View view) {
TextView email_TextView = (TextView) view.findViewById(R.id.register_email_input);
TextView pass1_TextView = (TextView) view.findViewById(R.id.register_pass1_input);
TextView pass2_TextView = (TextView) view.findViewById(R.id.register_pass2_input);
String email = email_TextView.getText().toString();
String pass1 = pass1_TextView.getText().toString();
String pass2 = pass2_TextView.getText().toString();
webGet.waitGet(
"https",
"...",
"/app/menu/data/register/",
"email="+webGet.encode(email)
+"&pass1="+webGet.encode(pass1)
+"&pass2="+webGet.encode(pass2),
new RegisterResponseHandler(),
toaster
);
}
public class RegisterResponseHandler
extends DWAWebGet.Handler<DWAWebGet.Response> {
public RegisterResponseHandler () {
new DWAWebGet().super();
}
public void handle (DWAWebGet.Response r) {
String status;
String error;
if ((status = r.getMap("status").getStr()) == null) {
toaster.error("Response Status: null");
} else if (status == "denied") {
if ((error = r.getMap("error").getStr()) == null) error = "No Reason Given";
toaster.alert("Registration Denied: " + error);
} else if (status != "good") {
if ((error = r.getMap("error").getStr()) == null) error = "blank";
toaster.error("Response Status: " + status + ", Error: "+error);
} else {
toaster.msg("Registration Complete. Please check your email and verify your email address.");
((MainActivity) SectionFragment_sign_in .this.getActivity())
.prefs_edit.putString("id_email",r.getMap("email").getStr());
((TextView) ((MainActivity) SectionFragment_sign_in .this.getActivity())
.findViewById(R.id.sign_in_email_input))
.setText(r.getMap("email").getStr());
}
}
}
}
}
public类MainActivity扩展了FragmentActivity
实现ActionBar.OnNavigationListener{
私有静态DWAWebGet=new DWAWebGet();
专用静态DWATOSTER烤面包机=
新的DWatoMaster(getApplicationContext())
.makeToast(“错误”,Toast.LENGTH\u LONG,重力。顶部,0,0)
.makeToast(“警报”,Toast.LENGTH\u SHORT,Gravity.TOP,0,0)
.makeToast(“味精”,吐司。长度较短,重力。顶部,0,0);
...
扩展片段中的公共静态类SectionFragment\u sign\u{
...
公共作废点击登记按钮(查看){
TextView email\u TextView=(TextView)view.findViewById(R.id.register\u email\u输入);
TextView pass1\u TextView=(TextView)view.findViewById(R.id.register\u pass1\u输入);
TextView pass2\u TextView=(TextView)view.findViewById(R.id.register\u pass2\u输入);
字符串email=email_TextView.getText().toString();
字符串pass1=pass1\u TextView.getText().toString();
字符串pass2=pass2u TextView.getText().toString();
webGet.waitGet(
“https”,
"...",
“/app/menu/data/register/”,
“email=“+webGet.encode(电子邮件)
+“&pass1=“+webGet.encode(pass1)
+“&pass2=“+webGet.encode(pass2),
新的RegisterResponseHandler(),
烤面包机
);
}
公共类RegisterResponseHandler
扩展DWAWebGet.Handler{
公共注册表响应管理器(){
新的DWAWebGet().super();
}
公共无效句柄(DWAWebGet.r){
字符串状态;
字符串错误;
if((status=r.getMap(“status”).getStr())==null){
toaster.error(“响应状态:null”);
}否则,如果(状态==“拒绝”){
如果((error=r.getMap(“error”).getStr())==null)error=“未给出原因”;
toaster.alert(“注册被拒绝:+错误”);
}否则,如果(状态!=“良好”){
如果((error=r.getMap(“error”).getStr())==null)error=“blank”;
烤面包机错误(“响应状态:+状态+”,错误:+错误);
}否则{
toaster.msg(“注册完成。请检查您的电子邮件并验证您的电子邮件地址。”);
((MainActivity)SectionFragment_sign_in.this.getActivity())
.prefs_edit.putString(“id_email”,r.getMap(“email”).getStr());
((TextView)((MainActivity)SectionFragment_sign_in.this.getActivity())
.findViewById(R.id.sign\u in\u email\u input))
.setText(r.getMap(“email”).getStr();
}
}
}
}
}
新建DWAToaster(MainActivity.this)
。使用活动上下文。“不能在静态上下文中使用此项”您不能在静态类中使用此
项。您可以使用getActivity()
获取托管活动的上下文。当我使用新的DWATOSTER()、使用DWATOSTER extends FRAMENT并将getActivity()放入DWATOSTER中时,它会起作用,但我不知道这是否是一个好方法,或者它是否能在我创建的任何应用程序中工作,而MainActivity不是一个碎片活动。