1

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).addOnAccountsUpdatedListener(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

user264953
  • 1,617
  • 8
  • 32
  • 57
  • 1
    possible duplicate of [Android: ProgressDialog.show() crashes with getApplicationContext](http://stackoverflow.com/questions/1561803/android-progressdialog-show-crashes-with-getapplicationcontext) – Octavian A. Damiean May 25 '11 at 11:02

2 Answers2

1

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

Egor
  • 37,239
  • 10
  • 109
  • 127
1

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.

ferostar
  • 6,936
  • 6
  • 35
  • 60
  • Hi Federico and Egor, thanks for your reply..but the syntax of the calling class does not permit me to pass 'this' or 'YourActivity.this' as it does not extend an activity. Here is my calling class definition - `class PatternUnlockScreen extends LinearLayoutWithDefaultTouchRecepient implements KeyguardScreen, KeyguardUpdateMonitor.InfoCallback, KeyguardUpdateMonitor.SimStateCallback` Pardon me for the improper formatting. – user264953 May 24 '11 at 17:54
  • Then you have a problem! No, really, this seems to be something like a bug. It happened allot to me, getting this exception by doing a getContext and solving it by just passing a reference to the Activity. And there is no other way to retrieve the Context or the currently running activity's context. All i can think off is for you to pass the Activity reference to your class's constructor or something like that, to make sure you are getting the correct context through all your method. – ferostar May 24 '11 at 18:01
  • I always get the same value, when I try to fetch the context using the different methods I told. Even I logged the output of the context object taken through three different approaches, and the three context objects were the same(and not null). I am really puzzled with this issue, as to what causes this error. – user264953 May 24 '11 at 18:16
  • This thing usually popus when showing simple AlertDialogs. Look http://stackoverflow.com/questions/1561803/android-progressdialog-show-crashes-with-getapplicationcontext. The solution provided, always: use this instead of get...(). Seems to be a big, big, ugly bug, because the context is not null at all. – ferostar May 24 '11 at 18:32
  • Shucks!! If this does not have a solution, then this is really nasty..I would be clamped at this point, to say the least!! – user264953 May 24 '11 at 18:54
  • Try doing as i told you. I suppose you are creating your class from an Activity, so pass a reference from it when you create it. Something like new PatternUnlockScreen(yourActivity.this). Then you will have a reference to the context. It might not be very elegant, but since nothing else seems to work... – ferostar May 24 '11 at 19:09
  • Ok Federico.. I need to try this and I will update you on the result.Thanks. – user264953 May 24 '11 at 19:19