我在另一个类中用于listview的Adapter类中有以下代码。
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Introduceti parola:");
final EditText input = new EditText(getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
hash.put("name", Rooms.name);
hash.put("parola", input.getText().toString());
site = siteul + "/join";
new ATask((ViewHolder) v.getTag()).execute(site);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
问题在于,当我触发操作时,我在builder.show()
行
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
我认为getContext()
有错,但它在同一个Adapter类中的代码中不会产生任何错误:
Toast.makeText(getContext(), "Cool message!", Toast.LENGTH_LONG).show();
那可能是什么问题?
答案 0 :(得分:1)
将getContext()
替换为ActivityName.this
。