1

这是我正在使用的课程

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;
}   

当我从此方法调用 asynctask 时出现错误。这是错误 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();         
}    

异步任务在这里

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;
    }
 }
4

1 回答 1

3

您不能在方法中实例化ColorsXMPP_AfterLogin类(我假设两者都在活动中)。问题在于它被声明为内部类,而内部类需要创建封闭类的实例(它们需要此连接)。在静态方法中,您没有此实例,因此编译器会抛出该错误。你有几个选项来解决这个问题,我推荐的一个是在(声明为)中作为嵌套类或将它完全移动到它自己的 java 文件中(如果你需要连接到'只需传递一个引用的构造函数中的那个)。staticalertContactsXmppColorsXMPP_AfterLoginalertColorsXMPP_AfterLoginContactsXmpppublic static class ColorsXMPP_AfterLogin...ActivityContextctxAsyncTask

您还可以使用该ctx变量来创建ColorsXMPP_AfterLogin类似的实例:

ctx.new ColorsXMPP_AfterLogin();
于 2013-01-01T09:32:40.530 回答