0

so I'm looking to create an application to control an arduino car , and i have a alert dialog that pops-up when i click a button , until here , everything works with no problems . But here i have a special thing , when I open the alert dialog by pressing it's button and let's say i press the power button to close my phone , my app crashes , and i tried a lot of things like PowerManager and stuff like that , but it didnt work

So here is the code for the Alert dialog :

void dialogCreater() {

            final AlertDialog.Builder mbuilder = new AlertDialog.Builder(CarActivity.this);
            View mView = getLayoutInflater().inflate(R.layout.settings, null);
            final EditText editForward = mView.findViewById(R.id.editForward);
            final EditText editBackwards = mView.findViewById(R.id.editBackwards);
            final EditText editLeft = mView.findViewById(R.id.editLeft);
            final EditText editRight= mView.findViewById(R.id.editRight);
            final EditText editForwardLeft = mView.findViewById(R.id.editForwardLeft);
            final EditText editForwardRight = mView.findViewById(R.id.editForwardRight);
            final EditText editBackwardsLeft = mView.findViewById(R.id.editBackwardsLeft);
            final EditText editBackwardsRight = mView.findViewById(R.id.editBackwardsRight);
            final EditText editStop = mView.findViewById(R.id.editStop);

            Button mAccept = mView.findViewById(R.id.btnAccept);
            Button mCancel = mView.findViewById(R.id.btnCancel);
            Button mDefaultSettings = mView.findViewById(R.id.btnDefault);
            mbuilder.setView(mView);

            dialog  = mbuilder.create();

            dialog.setCancelable(true);


           // a few buttons for the dialog 


           dialog.show();

Ok so this is the code , and my application crashes at the line from above (dialog.show()) when the dialog is opened and i close the phone This is the error I am getting :

 E/WindowManager: android.view.WindowLeaked: Activity com.example.edi.bluetoothgoogleplay.CarActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{5954400 V.E..... R....... 0,0-832,670} that was originally added here
                         at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
                         at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
                         at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
                         at android.app.Dialog.show(Dialog.java:298)
                         at com.example.edi.bluetoothgoogleplay.CarActivity.dialogCreater(CarActivity.java:624)
                         at com.example.edi.bluetoothgoogleplay.CarActivity$12.onClick(CarActivity.java:383)
                         at android.view.View.performClick(View.java:4756)
                         at android.view.View$PerformClick.run(View.java:19761)
                         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:5264)
                         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:900)
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)

So if anyone could help me , anything would be apreciated !

Sahdeep Singh
  • 1,342
  • 1
  • 13
  • 34
EDI ro
  • 3
  • 1
  • 3
  • Show us, where u r calling dialogCreater() – Sahdeep Singh Sep 18 '18 at 07:49
  • See this question https://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added – Sahdeep Singh Sep 18 '18 at 07:50
  • i added the last few lines and as i said , i call it in on create on a button – EDI ro Sep 18 '18 at 07:54
  • I think the easiest thing you can do is `dialog.dismiss()` in `onPause()` if `dialog != null` and `dialog.isShowing()`. I guess `dialog` is a class var within your activity, isn't it? I don't see it declared in your dialogCreater() method. – Juanvi Martinez Sep 18 '18 at 08:03
  • 1
    @SahdeepSingh thanks man , in your first answer i found that thing with onDestroy , i know i tryed it once in my project but it didn't work at that time , but now.... it does the job , and the app stops crashing ! – EDI ro Sep 18 '18 at 08:11
  • @JuanviMartinez thanks for your answer , i tried that too but , let me explain : the first time i open the dialog and then i close the phone the app works correctly , but when i try to open it up again , the button doesnt work anymore ... – EDI ro Sep 18 '18 at 08:13

0 Answers0