开发者

Error while passing Context

I am trying to pass context from one class to another.

Calling class:

  mForgotPatternButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            new ListOfAccounts(v.getContext());
        }
    });

Called Class:

public ListOfAccounts(Context context) {
     super(context);
     mAccounts = new ArrayList<String>();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Select the account");                    
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),R.layout.list_all_accounts, mAccounts);
        AccountManager.get(context).addOnAccounts开发者_开发技巧UpdatedListener(ListOfAccounts.this, null, true);              
        builder.setAdapter(adapter, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int item) {
                  mCallback.forgotPattern(true);
                   return;
               }
        });
        builder.create().show();            
    }

Instead of passing "v.getContext()", I have even tried giving "getContext()". But in all the cases, I get the following exception

05-24 16:11:27.087: ERROR/AndroidRuntime(4429): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

Kindly provide a solution for this.

Any help in this regard would be well appreciated.

Best Regards, Rony


Try passing YourActivityName.this instead of getContext(). Hope this helps.


Sometimes getContext() or even getApplicationContext() results in this exception. Try passing

this

or

YourActivity.this

Activity inherits from Context so it works, although i really don't know why getting the context as it should be done it won't work, giving that weird exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜