I am trying to show an AlertDialog when I click an item in a ListActivity. My application displays ListActivity under the TabActivity tab, and AlertDialog does not cause any problems. ListActivity (called FavoritesActivity) is pretty much straight from Android docs, with this setting:
lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
and then
public void confirmSend(final int position) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure?") .setCancelable(true) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) {
However, on the second tab of the TabActivity, I have an ActivityGroup that uses the LocalActivityManager to launch another ListActivity, for example (again, almost without changes from the tutorial on nested lists under online tabs):
public class MyGroupActivity extends ActivityGroup {
FirstListActivity is a ListActivity that is the first in a series. The user selects an item and is represented by another ListActivity with the following code:
lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(); intent.setClass(FirstListActivity.this, TheNextListActivity.class);
The last ListActivity in this series has EXACTLY the same onItemClick listener and the associated confirmSend function as the first ListActivity example that I showed (the one that works), but now when the user clicks on an item, AlertDialog cannot show and the application stops unexpectedly with this debug result:
W/WindowManager( 570): Attempted to add application window with unknown token android.os.BinderProxy@4373af30. Aborting. D/AndroidRuntime( 1953): Shutting down VM W/dalvikvm( 1953): threadid=3: thread exiting with uncaught exception (group=0x4000fe70) E/AndroidRuntime( 1953): Uncaught handler: thread main exiting due to uncaught exception E/AndroidRuntime( 1953): android.view.WindowManager$BadTokenException: Unable to add window
What is the difference between these two routes in AlertDialog that cause this failure? This seems to be related to the AlertDialog.Building (this) bit. When inside an ActivityGroup, it gets the wrong context or something like that. Each example that I found on the Internet for this error was resolved by changing between "this" and "getApplicationContext ()", but in this case none of them work. I tried other options for getting context, but since I mostly guessed at random, I thought it was better to ask for advice here. In what context should I go, or, what else is wrong?