Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid AlertDialog is causing a memory leak
    primarykey
    data
    text
    <p>Good day! Mr. Spock here.</p> <p>I have created an <code>ErrorListener</code> for my Android <code>MediaPlayer</code>. If I try to display an <code>AlertDialog</code> from my handler, it causes a "leaked window" exception to occur when the "OK" button is pressed on the dialog. Here are the relevant code snippets:</p> <p>Class definition:</p> <pre><code>public class PlayMediaActivity extends Activity implements OnPreparedListener, OnErrorListener, MediaController.MediaPlayerControl </code></pre> <p>Creating <code>MediaPlayer</code>:</p> <pre><code>mediaPlayer = new MediaPlayer(); mediaPlayer.setOnPreparedListener(this); mediaPlayer.setOnErrorListener(this); mediaController = new MediaController(this); try { mediaPlayer.setDataSource(URL); mediaPlayer.prepareAsync(); mediaPlayer.start(); } catch(...) {} // Omit real code for brevity. </code></pre> <p><code>MediaPlayer</code> prepared listener:</p> <pre><code>public void onPrepared(MediaPlayer mediaPlayer) { Log.d(TAG, "onPrepared"); mediaController.setMediaPlayer(this); mediaController.setAnchorView(findViewById(R.id.main_audio_view)); handler.post(new Runnable() { public void run() { mediaController.setEnabled(true); mediaController.show(); } }); } </code></pre> <p><code>MediaPlayer</code> error listener:</p> <pre><code>public boolean onError(MediaPlayer mp, int what, int extra) { ShowDialog("Unable to play audio. Try again later."); return true; } </code></pre> <p><code>ShowDialog</code> routine:</p> <pre><code>private void ShowDialog(String message) { // Alert the user something went wrong. ContextThemeWrapper cw = new ContextThemeWrapper( PlayMediaActivity.this, R.style.AlertDialogTheme ); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( cw ); alertDialogBuilder.setTitle("Oops!"); // set dialog message alertDialogBuilder .setMessage(message) .setCancelable(true) // Allow back hardkey to dismiss .setPositiveButton("OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, close current activity dialog.dismiss(); PlayMediaActivity.this.finish(); } }); alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { // Catch back key press. @Override public void onCancel(DialogInterface dialog) { //do whatever you want the back key to do dialog.dismiss(); PlayMediaActivity.this.finish(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); } </code></pre> <p>When I call <code>ShowDialog</code> from the <code>onError</code> routine, it pops up, but when I press the "OK" button the activity is stopped and destroyed but the dialog is leaked (according to the stack trace), even though I'm calling <code>dismiss()</code> before finishing the activity.</p> <p>There are quite a few stackoverflow posts on this kind of topic, but none have solved my problem. I'm pretty new to Android development and my guess is I'm doing something fundamentally wrong but I can't put my finger on it.</p> <p>Does anyone see what the problem might be?</p> <p>EDIT 1 - Including StackTrace:</p> <pre><code>04-14 14:38:08.089: E/WindowManager(2099): Activity com.lcboise.lifechurch.PlayMediaActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405619f8 that was originally added here 04-14 14:38:08.089: E/WindowManager(2099): android.view.WindowLeaked: Activity com.lcboise.lifechurch.PlayMediaActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405619f8 that was originally added here 04-14 14:38:08.089: E/WindowManager(2099): at android.view.ViewRoot.&lt;init&gt;(ViewRoot.java:259) 04-14 14:38:08.089: E/WindowManager(2099): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 04-14 14:38:08.089: E/WindowManager(2099): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 04-14 14:38:08.089: E/WindowManager(2099): at android.view.Window$LocalWindowManager.addView(Window.java:424) 04-14 14:38:08.089: E/WindowManager(2099): at android.app.Dialog.show(Dialog.java:241) 04-14 14:38:08.089: E/WindowManager(2099): at com.lcboise.lifechurch.PlayMediaActivity.ShowDialog(PlayMediaActivity.java:133) 04-14 14:38:08.089: E/WindowManager(2099): at com.lcboise.lifechurch.PlayMediaActivity.onError(PlayMediaActivity.java:237) 04-14 14:38:08.089: E/WindowManager(2099): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1456) 04-14 14:38:08.089: E/WindowManager(2099): at android.os.Handler.dispatchMessage(Handler.java:99) 04-14 14:38:08.089: E/WindowManager(2099): at android.os.Looper.loop(Looper.java:123) 04-14 14:38:08.089: E/WindowManager(2099): at android.app.ActivityThread.main(ActivityThread.java:3683) 04-14 14:38:08.089: E/WindowManager(2099): at java.lang.reflect.Method.invokeNative(Native Method) 04-14 14:38:08.089: E/WindowManager(2099): at java.lang.reflect.Method.invoke(Method.java:507) 04-14 14:38:08.089: E/WindowManager(2099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864) 04-14 14:38:08.089: E/WindowManager(2099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 04-14 14:38:08.089: E/WindowManager(2099): at dalvik.system.NativeStart.main(Native Method) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload