问题描述
|
我正在尝试将上下文从一个类传递到另一个类。
通话类:
mForgotPatternButton.setonClickListener(new OnClickListener() {
public void onClick(View v) {
new listofAccounts(v.getContext());
}
});
叫课:
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();
}
我什至没有尝试传递\“ v.getContext()\”,而是尝试传递\“ getContext()\”。但是在所有情况下,我都会收到以下异常
05-24 16:11:27.087: ERROR/AndroidRuntime(4429): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
请为此提供解决方案。
在这方面的任何帮助将不胜感激。
最好的祝福,
罗尼
解决方法
尝试传递
YourActivityName.this
而不是getContext()
。希望这可以帮助。
,有时,getContext()甚至getApplicationContext()都会导致此异常。尝试通过
this
要么
YourActivity.this
活动是从Context继承的,因此可以正常工作,尽管我真的不知道为什么要按需获取上下文却无法正常工作,这给我一个怪异的例外。