我创建了一个用于存储 toast 主题和创建 toast 的类。我目前正在一个片段中测试它,所以我想它需要是静态的。问题是我不知道如何传递应用程序上下文。我收到此错误:
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());
}
}
}
}
}