Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Adding Users Using Parse.com Adding a Progress Indicator

Tudor tud
Tudor tud
3,685 Points

error when showing the dialog box

The app is running, but then suddenly it stops with the following error message:

05-27 12:09:28.130 10353-10353/com.acelasi7.ribbit E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.acelasi7.ribbit, PID: 10353 android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@282a3a4a is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:562) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) at android.app.Dialog.show(Dialog.java:298) at com.acelasi7.ribbit.LoginActivity$2$1.done(LoginActivity.java:87) at com.acelasi7.ribbit.LoginActivity$2$1.done(LoginActivity.java:69) at com.parse.Parse$6$1.run(Parse.java:945) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Harry James
Harry James
14,780 Points

Hey Tudor!

What's at lines 69-87 in your LoginActivity file?

Could you please provide them here so that we can take a look?

Thanks in advance! :)

Tudor tud
Tudor tud
3,685 Points

This is what starts at line 69, and the 87 is the function call dialog.show();

ParseUser.logInInBackground(username, password, new LogInCallback() {
                        @Override
                        public void done(ParseUser user, ParseException e) {
                            setSupportProgressBarIndeterminateVisibility(false);

                            if(e == null){
                                // Success
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                                builder.setMessage(e.getMessage());
                                builder.setTitle(R.string.login_error_title);
                                builder.setPositiveButton(android.R.string.ok, null);

                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    });

1 Answer

Harry James
Harry James
14,780 Points

Thanks Tudor!

That code looks fine - it seems as though this is being caused because we're still in a background thread.

Luckily, we can go ahead and run this on the Main UI Thread like this:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
            builder.setMessage(e.getMessage())
        builder.setTitle(R.string.login_error_title)
        builder.setPositiveButton(android.R.string.ok, null);

            AlertDialog dialog = builder.create();
            dialog.show();
    }
});

Note that you will also need to declare the e variable as final.


Hope it helps and let me know how it goes :)