问题描述
这是我使用的是类
公共类ContactsXmpp扩展SherlockFragmentActivity {私有静态上下文CTX;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.contacts_xmpp_sip); CTX =这一点;
}
我得到的错误,当我从这个方法调用的AsyncTask。这是错误类型ContactsXmpp没有外围实例访问。必须限定型ContactsXmpp(例如x.new A(),其中x是ContactsXmpp的一个实例)的类实例分配。
私有静态无效警报(字符串str,最终字符串名称){ AlertDialog.Builder alertDialogBuilder =新AlertDialog.Builder(CTX);
alertDialogBuilder.setMessage(STR +:+名称);
alertDialogBuilder.setCancelable(假)
.setPositiveButton(是,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
订阅(名);
新ColorsXMPP_AfterLogin()执行(); /// **错误让这里..
}
}); alertDialogBuilder.setNegativeButton(否,新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释它){
退订(名);
}
}); AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
在这里的AsyncTask
公共类ColorsXMPP_AfterLogin扩展的AsyncTask< AfterLogging,太虚,太虚> { 私人ProgressDialog _dialog; @覆盖
在preExecute保护无效(){
super.on preExecute();
Log.e(TAGGmailXMPP_AfterLogin在preExecute);
} @覆盖
保护无效onPostExecute(无效供稿){
// ProgressBar_hide();
_dialog.dismiss();
Log.e(TAGGmailXMPP_AfterLogin onPostExecute);
} @覆盖
保护无效doInBackground(AfterLogging ......为arg0){ Log.e(TAGGmailXMPP_AfterLogin doInBackground); 返回null;
}
}
您不能实例在静态$的
ColorsXMPP_AfterLogin
类C $ C> 警报的方式
(我假设都是在 ContactsXmpp
活动)。问题是, ColorsXMPP_AfterLogin
被声明为一个的的内部类的和内部类需要创建封装类的一个实例(他们需要这种连接)。在静态警报
方法,你不这样编译器会引发这个错误有这样的实例。您有几种选择来解决这个问题,一个我会建议是要么让 ColorsXMPP_AfterLogin
在 ContactsXmpp嵌套类
(声明如公共静态类ColorsXMPP_AfterLogin ...
),或者完全它自己的Java文件移动(如果你需要在活动的连接
的上下文
简单地传递给一个参考 CTX
在 AsyncTask的
的构造函数)。
您也可以使用 CTX
变量来创建 ColorsXMPP_AfterLogin
像实例:
ctx.new ColorsXMPP_AfterLogin();
this is the class i am using
public class ContactsXmpp extends SherlockFragmentActivity {
private static Context ctx;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_xmpp_sip);
ctx = this;
}
i am getting error when i call asynctask from this method. this is the errorNo enclosing instance of type ContactsXmpp is accessible. Must qualify the allocation with an enclosing instance of type ContactsXmpp (e.g. x.new A() where x is an instance of ContactsXmpp).
private static void alert( String str, final String name ) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx);
alertDialogBuilder.setMessage(str + ": " + name);
alertDialogBuilder.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Subscription(name);
new ColorsXMPP_AfterLogin().execute(); ///** error getting here..
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
unSubscribe(name);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
Asynctask here
public class ColorsXMPP_AfterLogin extends AsyncTask<AfterLogging, Void, Void> {
private ProgressDialog _dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.e(TAG, " GmailXMPP_AfterLogin onPreExecute" );
}
@Override
protected void onPostExecute(Void feed) {
//ProgressBar_hide();
_dialog.dismiss();
Log.e(TAG, " GmailXMPP_AfterLogin onPostExecute" );
}
@Override
protected Void doInBackground(AfterLogging... arg0) {
Log.e(TAG, " GmailXMPP_AfterLogin doInBackground" );
return null;
}
}
You can't instantiate the ColorsXMPP_AfterLogin
class in the static
method of alert
(I'm assuming that both are in the ContactsXmpp
activity). The problem is that ColorsXMPP_AfterLogin
is declared as being an inner class and inner classes need an instance of the enclosing class to be created(they require this connection). In the static alert
method you don't have this instance so the compiler throws that error. You have several options to solve the problem, the one I would recommend is to either make ColorsXMPP_AfterLogin
as a nested class in ContactsXmpp
(declared like public static class ColorsXMPP_AfterLogin...
) or completely move it in its own java file(if you need a connection to the Activity
's Context
simply pass a reference to that ctx
in the AsyncTask
's constructor).
You could also use the ctx
variable to create the instance of ColorsXMPP_AfterLogin
like:
ctx.new ColorsXMPP_AfterLogin();
这篇关于类型ContactsXmpp没有外围实例可以访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!