AlertDialog cannot be displayed in ListActivity in ActivityGroup

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) { //... code to set the strings station, number, route, and direction FavouritesActivity.this.confirmSend(position); } }); 

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) { //... some code to run here } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } 

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 { // Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view public static MyGroupActivity group; // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory. private ArrayList<View> history; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.history = new ArrayList<View>(); group = this; // Start the root activity withing the group and get its view View view = getLocalActivityManager().startActivity("FirstListActivity", new Intent(this,FirstListActivity.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); // Replace the view of this ActivityGroup replaceView(view); } public void replaceView(View v) { // Changes this Groups View to the new View. setContentView(v); } 

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); // Create View using the Group Activity LocalActivityManager View newview = MyGroupActivity.group.getLocalActivityManager() .startActivity("show_routes", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); // And replace the view MyGroupActivity.group.replaceView(newview); } }); 

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 -- token android.app.LocalActivityManager$LocalActivityRecord@43750f98 is not valid; is your activity running? E/AndroidRuntime( 1953): at android.view.ViewRoot.setView(ViewRoot.java:425) E/AndroidRuntime( 1953): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:178) E/AndroidRuntime( 1953): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) E/AndroidRuntime( 1953): at android.view.Window$LocalWindowManager.addView(Window.java:392) E/AndroidRuntime( 1953): at android.app.Dialog.show(Dialog.java:231) E/AndroidRuntime( 1953): at com.ttcsms.LastListActivity.confirmSend(LastListActivity.java:119) E/AndroidRuntime( 1953): at com.ttcsms.LastListActivity$1.onItemClick(LastListActivity.java:66) E/AndroidRuntime( 1953): at android.widget.AdapterView.performItemClick(AdapterView.java:283) E/AndroidRuntime( 1953): at android.widget.ListView.performItemClick(ListView.java:3132) E/AndroidRuntime( 1953): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1620) E/AndroidRuntime( 1953): at android.os.Handler.handleCallback(Handler.java:587) E/AndroidRuntime( 1953): at android.os.Handler.dispatchMessage(Handler.java:92) E/AndroidRuntime( 1953): at android.os.Looper.loop(Looper.java:123) E/AndroidRuntime( 1953): at android.app.ActivityThread.main(ActivityThread.java:3948) E/AndroidRuntime( 1953): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime( 1953): at java.lang.reflect.Method.invoke(Method.java:521) E/AndroidRuntime( 1953): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) E/AndroidRuntime( 1953): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) E/AndroidRuntime( 1953): at dalvik.system.NativeStart.main(Native Method) I/Process ( 570): Sending signal. PID: 1953 SIG: 3 I/dalvikvm( 1953): threadid=7: reacting to signal 3 I/dalvikvm( 1953): Wrote stack trace to '/data/anr/traces.txt' I/Process ( 1953): Sending signal. PID: 1953 SIG: 9 I/ActivityManager( 570): Process com.ttcsms (pid 1953) has died. 

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?

+4
source share
4 answers

Got! This is actually an Android Context issue.

In line:

AlertDialog.Builder builder = new AlertDialog.Builder ( this );

Instead , you should pass the TabActivity Context, the visible / main activity context is passed to display any popup in the action.

So a simple solution:

  • Keep TabActivity context for later use in popup window.
  • Skip the TabActivity Context tab in the pop-up code instead.

hope this helps. greetings :)

+21
source

Finally your approach followed and solved my problem. Here I give some details

create the context of the Tabactivity class as follows.

"public static context MyTabactivity;

assgin "context = this" in the onCreate method MyTabactivity.
Then use MyTabactivity.context in your alerts as follows.

"AlertDialog.Builder adb = new AlertDialog.Builder (MyTabactivity.context);"

Hope this helps .....

+5
source

To make it pretty simple, instead of defining a context variable and setting it to onCreate , you can directly pass getParent() as the context to AlertDialog .

+5
source

I just want to listen and say that the above answer did not work on us for me (when I get back to the whole application, I will see the same old crash). Instead, the work used parental context. See also:

How to show an alert within an action group?

+1
source

Source: https://habr.com/ru/post/1338525/


All Articles