类型ContactsXmpp没有外围实例可以访问? [英] No enclosing instance of type ContactsXmpp is accessible?

查看:131
本文介绍了类型ContactsXmpp没有外围实例可以访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用的是类

 公共类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 alertDialogBu​​ilder =新AlertDialog.Builder(CTX);
    alertDialogBu​​ilder.setMessage(STR +:+名称);
    alertDialogBu​​ilder.setCancelable(假)
    .setPositiveButton(是,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释的id){
            订阅(名);
            新ColorsXMPP_AfterLogin()执行(); /// **错误让这里..
        }
    });    alertDialogBu​​ilder.setNegativeButton(否,新DialogInterface.OnClickListener(){
        @覆盖
        公共无效的onClick(DialogInterface对话,诠释它){
            退订(名);
        }
    });    AlertDialog alertDialog = alertDialogBu​​ilder.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;
    }
 }


解决方案

您不能实例在静态 Col​​orsXMPP_AfterLogin 类C $ C> 警报的方式(我假设都是在 ContactsXmpp 活动)。问题是, Col​​orsXMPP_AfterLogin 被声明为一个的的内部类的和内部类需要创建封装类的一个实例(他们需要这种连接)。在静态警报方法,你不这样编译器会引发这个错误有这样的实例。您有几种选择来解决这个问题,一个我会建议是要么让 Col​​orsXMPP_AfterLogin ContactsXmpp嵌套类(声明如公共静态类ColorsXMPP_AfterLogin ... ),或者完全它自己的Java文件移动(如果你需要在活动的连接上下文简单地传递给一个参考 CTX AsyncTask的的构造函数)。

您也可以使用 CTX 变量来创建 Col​​orsXMPP_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 error No 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没有外围实例可以访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆